网站地图    收藏   

主页 > 后端 > 正则表达式 >

Javascript Validation for email(正则表达式) 英文翻译

来源:自学PHP网    时间:2014-10-28 17:28 作者: 阅读:

[导读] 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/

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论