Input常用的输入框验证(正则)(转载)

328 阅读1分钟

只能输入英文
<input type="text" onkeyup="value=value.replace(/[^a-zA-Z]/g,'')">
身份证号只能输入数字和英文x
<input type="text" onkeyup="value=value.replace(/[^\d\x\X]/g,'')">
只能输入数字,小数点:
<input type="text" onkeyup="value=value.replace(/[^\d\.]/g,'')">
不能为空
<input onblur="if(this.value.replace(/^ +| +$/g,'')=='')alert('不能为空!')">
只能输入中文
<input type="text" onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')">
验证邮箱
<input type="submit" class="stdbtn" onclick="return checkEmail()"/>

function checkEmail(e){
        console.log(e)
        var $email=$("#email").val();
        if(!$email.match(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/)){
            alert("格式不正确!请重新输入");
            $("#email").focus();
            return false;
        }
 
 }


          
        
    

    

    

    
      
        

作者:鹧鸪少
链接:https://www.jianshu.com/p/8da1e2eae778
来源:简书