`
hwaspf
  • 浏览: 8291 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
最近访客 更多访客>>
社区版块
存档分类
最新评论

数字,Email,长度验证

阅读更多
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<script type="text/javascript">
validateNumber = function(id) {
    var input = document.getElementById(id); 
    input.onkeypress = function() {
   return event.keyCode >= 48 && event.keyCode <= 57
       || event.keyCode == 45 || event.keyCode == 43;
    }
    input.onbeforepaste = function() {
       clipboardData.setData('text', clipboardData.getData('text').replace(
       /[^-+\d]/g, ''));
    }
    input.style.imeMode = "disabled";
}

validateEmail = function(id){
    var input = document.getElementById(id); 
    var emailRegex = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    var emailRegex2 = /^(?:\w+.?)*\w+@(?:\w+\.?)+\w+$/;
    if(!emailRegex.test(input.value)){
       alert("Email格式不对!");
     input.value="";
    }
}

validateLength = function(id,len){
    var input = document.getElementById(id);
    if(input.value.length<len)
    alert("密码长度必须大于"+len+"位");
}
</script>
</HEAD>

<BODY>
<br><a>数字输入框:</a>
<input id="number" value="" onclick="validateNumber('number')">
<br>
<br><a>Email输入框:</a>
<input id="email" value="" onblur="validateEmail('email')">
<br>
<br><a>密码输入框:</a>
<input type="password" id="length" value="" onblur="validateLength('length',6)">
</BODY>
</HTML>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics