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

Ianthe ·
更新时间:2024-11-13
· 841 次阅读

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/ 您可能感兴趣的文章:JS正则验证多个邮箱完整实例【邮箱用分号隔开】JS正则表达式验证账号、手机号、电话和邮箱是否合法js与jquery正则验证电子邮箱、手机号、邮政编码的方法日常收集JS邮箱验证正则表达式JS正则验证邮箱的格式详细介绍比较正宗的验证邮箱的正则表达式js代码详解JavaScript常用正则验证函数实例小结【年龄,数字,Email,手机,URL,日期等】利用js正则表达式验证手机号,email地址,邮政编码js验证email的正则JavaScript email邮箱/邮件地址的正则表达式及分析



fo 正则表达式 email em 正则 pt asc ida id for script ip mail rip al sc io li ema

需要 登录 后方可回复, 如果你还没有账号请 注册新账号