如何使用正则表达式验证文本框值例如验证电子邮件,电话号码,字符串值
答案 0 :(得分:0)
您可以使用此jquery http://jqueryvalidation.org/
这个jQuery插件简化了客户端表单验证
或强>
电子邮件验证:
<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>');
}
});