文字框驗證正則表示式
阿新 • • 發佈:2018-12-27
只能輸入數字:
<input type="text" id="e_Phone" name="e_Phone"
onkeyup="this.value=this.value.replace(/\D/g,'')"
onafterpaste="this.value=this.value.replace(/\D/g,'')"
onblur="checkePhone()"/>
只能輸入中文漢字:
<input type="text" onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')"
onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"/>
只能輸入英文和數字
<input type="text" onkeyup="value=value.replace(/[\W]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">
只能輸入數字,小數點,下劃線:
<input type="text" name="price"
onkeyup="value=value.replace(/[^\d\._]/g,'')">
只能輸入中文,英文,數字,@符號,.符號和!符號:
<textarea name="e_Description" id="e_Description" class="layui-textarea" onkeyup="value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\@\.\!]/g,'')"></textarea>
非空驗證:
function checkCode() {
var e_Code = $("#e_Code").val();//拿到input文字框值
if (e_Code.trim().length <= 0) { //判斷值長度(去空格)小於0
layer.msg("員工編碼不能為空!"); //輸出提示
return false;
}
return true;
}
驗證姓名文字框不能輸入數字
function checkeRealName() {
var e_RealName = $("#e_RealName").val();
for (var i = 0; i < e_RealName.length; i++) {
var j = e_RealName.substring(i, i + 1);
if (j > 0) {
layer.msg("姓名中不能包含數字!")
return false;
}
}
if (e_RealName.trim().length <= 0) {
layer.msg("姓名不能為空!");
return false;
}
return true;
}
驗證手機號文字框格式:
function checkePhone() {
var e_Phone = $("#e_Phone").val();
var reg = /^1[345789]\d{9}$/;
if (reg.test(e_Phone)==false) {
layer.msg("手機號碼格式不正確,只能以13,14,15,17,18,19為開頭的11位數字");
return false;
}
if (e_Phone.trim().length <= 0) {
layer.msg("員工電話不能為空!");
return false;
}
return true;
}
驗證郵箱文字框格式:
function checkEmail(){
var email = $("#email").val();
if(email.trim().length<=0){
$("#DivEmail").html("電子郵箱不能為空")
return false;
}
if(email.indexOf("@")==-1){
$("#DivEmail").html("電子郵箱中必須包含符合@");
return false;
}
if(email.indexOf(".")==-1){
$("#DivEmail").html("電子郵箱中必須包含符合.");
return false;
}
return true;
}
驗證密碼長度:
function checkPass(){
var pass = $("#pass").val();
if(pass.trim().length<6){
$("#DivPwd").html("密碼必須大於等於6位");
return false;
}
return true;
}
驗證兩次密碼是否相同:
function checkrPass(){
var pass = $("#pass"); //第一次輸入的密碼
var rp = $("#rpass"); //確認密碼
if(pass.val()!=rp.val()){
$("#DivRepwd").html("兩次輸入的密碼不一致");
return false;
}
return true;
}