Try testing the following form with valid and invalid email addresses. The
code uses javascript to match the users input with a regular expression.
函數(shù)代碼:
復(fù)制代碼 代碼如下:
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')
使用方法:
復(fù)制代碼 代碼如下:
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正則驗(yàn)證多個(gè)郵箱完整實(shí)例【郵箱用分號(hào)隔開(kāi)】
- JS正則表達(dá)式驗(yàn)證賬號(hào)、手機(jī)號(hào)、電話和郵箱是否合法
- js與jquery正則驗(yàn)證電子郵箱、手機(jī)號(hào)、郵政編碼的方法
- 日常收集JS郵箱驗(yàn)證正則表達(dá)式
- JS正則驗(yàn)證郵箱的格式詳細(xì)介紹
- 比較正宗的驗(yàn)證郵箱的正則表達(dá)式j(luò)s代碼詳解
- JavaScript常用正則驗(yàn)證函數(shù)實(shí)例小結(jié)【年齡,數(shù)字,Email,手機(jī),URL,日期等】
- 利用js正則表達(dá)式驗(yàn)證手機(jī)號(hào),email地址,郵政編碼
- js驗(yàn)證email的正則
- JavaScript email郵箱/郵件地址的正則表達(dá)式及分析