【发布时间】:2014-11-05 22:26:46
【问题描述】:
我有一个字段,我针对三个不同的用户输入验证对其进行验证。我已经在服务器端 C# 上完成了它,但我也需要在 Jquery/Javascript 中完成它,因为这些文本框位于页面上的对话框中:
if (part.RevisionNumber != null)
{
bool isPartRevNA = false;
//Check if Revision Number = NA or N\A
if(part.RevisionNumber == "NA" || part.RevisionNumber == @"N\A")
isPartRevNA = true;
if (part.RevisionNumber.Length > 4)
{
ModelState.AddModelError(string.Empty, "Revision# is an optional field but the format must be :3-digit, numeric values only (e.g., ‘102’) or 2. 4-digit, 3 numeric values plus 1 alpha (e.g., ‘102B’) or 3. Character string to represent not applicable (e.g., ‘NA’, or ‘N/A’). ");
isPartRevNA = false;
}
if (!isPartRevNA)
{
//revision number not na or n\a
//1. check next regex condition of if 3 digit numeric value
if (part.RevisionNumber.Length == 3)
{
string pattern = @"\A(\d){3}\Z";
Regex rgx = new Regex(pattern);
bool match = rgx.IsMatch(part.RevisionNumber);
if (!match)
ModelState.AddModelError(string.Empty, "Revision# is an optional field but the format must be :3-digit, numeric values only (e.g., ‘102’) or 2. 4-digit, 3 numeric values plus 1 alpha (e.g., ‘102B’) or 3. Character string to represent not applicable (e.g., ‘NA’, or ‘N/A’). ");
}
//2. check regex condition if length 4
if (part.RevisionNumber.Length == 4)
{
string pattern = @"\d{3}[A-Za-z]";
Regex rgx = new Regex(pattern);
bool match = rgx.IsMatch(part.RevisionNumber);
if (!match)
ModelState.AddModelError(string.Empty, "Revision# is an optional field but the format must be :3-digit, numeric values only (e.g., ‘102’) or 2. 4-digit, 3 numeric values plus 1 alpha (e.g., ‘102B’) or 3. Character string to represent not applicable (e.g., ‘NA’, or ‘N/A’). ");
}
}
}
该字段是可选的,但在以下事件中,我将在 textarea 聚焦事件中捕获用户输入,如果它有一个值并且不符合用户条件,那么我将重置该值并提醒用户正确输入应该他们选择:
1) 可能是 NA 或 N\A 2) 3 位,仅限数值(例如,‘102’),@"\A(\d){3}\Z"; 3) 4 位,3 个数值加上 1 个字母(例如,‘102B’)@"\d{3}[A-Za-z]";
$(".foo").focusout( function() {
alert( $(this).val() );
}).click(function (e) {
e.stopPropagation();
return true;
});
【问题讨论】:
标签: javascript c# jquery