Javascript Validation for email(正则表达式) 英文翻译
更新时间:2011年10月04日 19:52:14 作者:
javascript中通过正则表达式验证email地址是否符合规则,需要的朋友可以参考下。
Try testing the following form with valid and invalid email addresses. The
code uses javascript to match the users input with a regular expression.
函数代码:
function validate(form_id,email) {
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var address = document.forms[form_id].elements[email].value;
if(reg.test(address) == false) {
alert('Invalid Email Address');
return false;
}
}
In the forms ‘onsubmit' code call javascript:return validate(‘form_id','email_field_id')
使用方法:
<form id="form_id" method="post" action="action.php" onsubmit="javascript:return validate('form_id','email');">
<input type="text" id="email" name="email" />
<input type="submit" value="Submit" />
</form>
You should not rely purely on client side validation on your website / web application, if the user has javascript disabled this will not work. Always validate on the server.
from: http://www.white-hat-web-design.co.uk/blog/javascript-validation/
code uses javascript to match the users input with a regular expression.
函数代码:
复制代码 代码如下:
function validate(form_id,email) {
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var address = document.forms[form_id].elements[email].value;
if(reg.test(address) == false) {
alert('Invalid Email Address');
return false;
}
}
In the forms ‘onsubmit' code call javascript:return validate(‘form_id','email_field_id')
使用方法:
复制代码 代码如下:
<form id="form_id" method="post" action="action.php" onsubmit="javascript:return validate('form_id','email');">
<input type="text" id="email" name="email" />
<input type="submit" value="Submit" />
</form>
You should not rely purely on client side validation on your website / web application, if the user has javascript disabled this will not work. Always validate on the server.
from: http://www.white-hat-web-design.co.uk/blog/javascript-validation/
相关文章
正则表达式语法规则及在Javascript和C#中的使用方法
正则表达式通常被用来检索和/或替换那些符合某个模式的文本内容。许多程序设计语言都支持利用正则表达式进行字符串操作2013-10-10js中exec、test、match、search、replace、split用法
exec、test、match、search、replace、split在JS中用的很频繁,在网上看到对这些方法的总结,就转过来了,作个记录2012-08-08javascript正则表达式和字符串RegExp and String(二)
这篇文章主要介绍了javascript正则表达式和字符串RegExp and String的相关资料,需要的朋友可以参考下2015-10-10
最新评论