本文實(shí)例為大家分享了js正則表達(dá)式驗(yàn)證輸入合法性的具體代碼,供大家參考,具體內(nèi)容如下
// 驗(yàn)證用戶名,只允許 字母 數(shù)字 下劃線 中文//在input元素后面增加一個(gè)span元素,提示function confirmName(){ var name=document.getElementById("user_name"); name.onblur=function(){ if((name.value).length!=0){ reg=/^[/u4e00-/u9fa5_a-zA-Z0-9-]{1,16}$/g; if(!reg.test(name.value)){ alert("對(duì)不起,輸入的用戶名限16個(gè)字符,支持中英文、數(shù)字、減號(hào)或下劃線 "); } } };}function confirmEmail(){ var email=document.getElementById("user_email"); email.onblur=function(){ if((email.value).length!=0){ reg=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/ ; if(!reg.test(email.value)){ alert("對(duì)不起,您輸入的郵箱格式不正確!"); } } };}function confirmPassword(){ var password=document.getElementById("user_password"); password.onblur=function(){ if((password.value).length!=0){ reg=/^(/w){6,20}$/; if(!reg.test(password.value)){ alert("對(duì)不起,您輸入的密碼格式不正確!"); } } };}function confiemPasswordAgin(){ var againpassword=document.getElementById("user_password_confirmation"); var password=document.getElementById("user_password"); againpassword.onblur=function(){ // alert(againpassword.value); if(password.value.length!=againpassword.value.length){ alert("密碼輸入不匹配,請(qǐng)重新輸入"); } if(password.value.indexOf(againpassword.value)==-1){ alert("密碼輸入不匹配,請(qǐng)重新輸入"); } };}window.onload=function(){ confirmName(); confirmEmail(); confirmPassword(); confiemPasswordAgin();};以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。
新聞熱點(diǎn)
疑難解答