Javascript Validation for email(正則表達式) 英文翻譯
更新時間:2011年10月04日 19:52:14 作者:
javascript中通過正則表達式驗證email地址是否符合規(guī)則,需要的朋友可以參考下。
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ù)代碼:
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.
函數(shù)代碼:
復制代碼 代碼如下:
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/
相關文章
限制文本框中只能輸入實數(shù)或整數(shù),其它字符無效,有勞大家了!
限制文本框中只能輸入實數(shù)或整數(shù),其它字符無效,有勞大家了!...2006-10-10正則表達式語法規(guī)則及在Javascript和C#中的使用方法
正則表達式通常被用來檢索和/或替換那些符合某個模式的文本內容。許多程序設計語言都支持利用正則表達式進行字符串操作2013-10-10js中exec、test、match、search、replace、split用法
exec、test、match、search、replace、split在JS中用的很頻繁,在網(wǎng)上看到對這些方法的總結,就轉過來了,作個記錄2012-08-08javascript正則表達式和字符串RegExp and String(二)
這篇文章主要介紹了javascript正則表達式和字符串RegExp and String的相關資料,需要的朋友可以參考下2015-10-10