2014-11-05 49 views
-3

我有一个字段,我针对三种不同的用户输入验证进行了验证。我已经做了服务器端的C#,但我需要做的是jQuery的/ JavaScript的,以及因为这些文本框在页面上一个对话框:将c#正则表达式转换为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个alpha(例如'102B')@“\ d {3} [A-Za-z]”;

$(".foo").focusout(function() { 


       alert($(this).val()); 
     }).click(function (e) { 
      e.stopPropagation(); 
      return true; 
     }); 

回答

0

下面的正则表达式应该做自己的工作,并将努力在.NET和JS:

^(?:N\\?A|\d{3}[A-Za-z]?)$ 

这很简单:

  • 无论是比赛NA,可选\之间
  • 或3位数字后跟一个可选的字母。
相关问题