Javascript Validation for 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/
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!!
点击进行留言回复
网友评论