2013-12-14 108 views
-1

如何使用正则表达式例如验证电子邮件,电话号码,以验证文本框中的值,字符串值jQuery验证文本框valuess

+0

在网上提供的jQuery插件已经很多,这可能会满足您的需要 –

+0

方式太不具体。通过代码示例给出具体的问题,这些问题表明您在尝试解决这个问题方面花费更多精力,而不是在键盘中输入60个字符。 – dthree

回答

0

你可以使用这个jQuery http://jqueryvalidation.org/

这个jQuery插件使简单的客户方形式验证容易

DEMO

电子邮件验证:

<script type = "text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
    <script type = "text/javascript"> 
     function ValidateEmail(email) { 
      var expr = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; 
      return expr.test(email); 
     }; 
     $("#demo").live("click", function() { 
      if (!ValidateEmail($("#txtEmail").val())) { 
       alert("Invalid email address."); 
      } 
      else { 
       alert("Valid email address."); 
      } 
     }); 
    </script> 
    <input type = "text" id = "txtEmail" /> 
<input type = "button" id = "demo" value = "Demo" /> 

为数字字符

$('.keyup-numeric').keyup(function() { 
    $('span.error-keyup-1').hide(); 
    var inputVal = $(this).val(); 
    var numericReg = /^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$/; 
    if(!numericReg.test(inputVal)) { 
     $(this).after('<span class="error error-keyup-1">Numeric characters only.</span>'); 
    } 
}); 
+0

我需要验证码和正常的表达 –

+0

我编辑了我的答案.. !! –

+0

对不起朋友现在只有我得到了15个声望 –