验证无效的10位移动号码和焦点输入字段

时间:2014-03-17 06:06:17

标签: javascript php jquery html5

我需要在jQuery中验证电子邮件和手机号码的代码,以及在不满足验证的特定字段上focus()

这是我的查询

<form name="enquiry_form" method="post" id="enquiry_form">

    Full Name *
    <input class="input-style" name="name"  id="name" type="text" required> 
    Email *
    <input class="input-style" name="email"  id="email" type="email" required>
    Phone * 
    <input name="mobile"  id="mobile" type="number" required>

    <input type="submit" value="SUBMIT"  id="enq_submit"">

</form>

11 个答案:

答案 0 :(得分:4)

以上代码是正确的,但移动验证并不完美。

我修改为

$('#enquiry_form').validate({
  rules:{
  name:"required",
  email:{
  required:true,
  email:true
  },
  mobile:{
      required:true,
  minlength:9,
  maxlength:10,
  number: true
  },
  messages:{
  name:"Please enter your username..!",
  email:"Please enter your email..!",
      mobile:"Enter your mobile no"
  },
  submitHandler: function(form) {alert("working");
  //write your success code here  
  }
  });

答案 1 :(得分:3)

我使用$ form.submit(),因为它保持html输入验证。

我也使用移动浏览器支持的输入类型tel,只显示数字小键盘。

FIRClearcutLogger

答案 2 :(得分:2)

对于电子邮件验证,<input type="email">已足够..

for mobile no use pattern属性输入如下:

<input type="number" pattern="\d{3}[\-]\d{3}[\-]\d{4}" required>

您可以在http://html5pattern.com上查看更多模式。

关注字段,您可以使用onkeyup()事件:

function check()
{

    var pass1 = document.getElementById('mobile');


    var message = document.getElementById('message');

   var goodColor = "#0C6";
    var badColor = "#FF9B37";

    if(mobile.value.length!=10){

        mobile.style.backgroundColor = badColor;
        message.style.color = badColor;
        message.innerHTML = "required 10 digits, match requested format!"
    }}

,您的HTML代码应为:

<input name="mobile"  id="mobile" type="number" required onkeyup="check(); return false;" ><span id="message"></span>

答案 3 :(得分:2)

您也可以使用jquery

  var phoneNumber = 8882070980;
            var filter = /^((\+[1-9]{1,4}[ \-]*)|(\([0-9]{2,3}\)[ \-]*)|([0-9]{2,4})[ \-]*)*?[0-9]{3,4}?[ \-]*[0-9]{3,4}?$/;

            if (filter.test(phoneNumber)) {
              if(phoneNumber.length==10){
                   var validate = true;
              } else {
                  alert('Please put 10  digit mobile number');
                  var validate = false;
              }
            }
            else {
              alert('Not a valid number');
              var validate = false;
            }

         if(validate){
          //number is equal to 10 digit or number is not string 
          enter code here...
        }

答案 4 :(得分:1)

测试所有答案后没有成功。有时输入也会带有字母字符。

这是最后一个完整的工作代码,只输入数字,如果数字不正确,请记住用户的退格键按键事件。

&#13;
&#13;
$("#phone").keydown(function(event) {
  k = event.which;
  if ((k >= 96 && k <= 105) || k == 8) {
    if ($(this).val().length == 10) {
      if (k == 8) {
        return true;
      } else {
        event.preventDefault();
        return false;

      }
    }
  } else {
    event.preventDefault();
    return false;
  }

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="phone" id="phone" placeholder="Mobile Number" class="form-control" type="number" required>
&#13;
&#13;
&#13;

答案 5 :(得分:0)

答案 6 :(得分:0)

在脚本代码DEMO

中使用jquery validator
<script src="js/jquery.validate.min.js"></script> 

并像这样验证您的元素

   <form name="enquiry_form" method="post" id="enquiry_form">

    Full Name *
    <input class="input-style" name="name"  id="name" type="text"/>
    <br>
    Email *
    <input class="input-style" name="email"  id="email" type="email"><br>
    Phone * 
        <input id="mobile" name="mobile"  id="mobile"></input><br>

    <input type="submit" value="SUBMIT"  id="enq_submit">

</form>

 $('#enquiry_form').validate({
      rules:{
      name:"required",
      email:{
      required:true,
      email:true
      },
      mobile:{
          required:true,
      minlength:9,
      maxlength:10,
      }
      },
      messages:{
      name:"Please enter your username..!",
      email:"Please enter your email..!",
          mobile:"Enter your mobile no"
      },

      submitHandler: function(form) {
alert("working");
      //write your success code here  
      }
      });

答案 7 :(得分:0)

function is_mobile_valid(string_or_number){
            var mobile=string_or_number;
            if(mobile.length!=10){
                return false;

            }
            intRegex = /[0-9 -()+]+$/;
            is_mobile=true;
            for ( var i=0; i < 10; i++) {
                if(intRegex.test(mobile[i]))
                     { 
                     continue;
                     }
                    else{
                        is_mobile=false;
                        break;
                    }
                 }
            return is_mobile;

}

您可以通过调用函数is_mobile_valid(your_string_of_mobile_number);

进行检查

答案 8 :(得分:0)

function isMobileNumber(evt, sender) { // Allows only 10 numbers // ONKEYPRESS FUNCTION
//evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
var NumValue = $(sender).val();
if (charCode > 31 && (charCode < 48 || charCode > 57)) {//|| NumValue.length > 9) {
    return false;
}
return true;}


function isProperMobileNumber(txtMobId) { //// VALIDATOR FUNCTION   
var txtMobile = document.getElementById(txtMobId);
if (!isNumeric(txtMobile.value)) { $(txtMobile).css("border-color", "red"); return false; }
if (txtMobile.value.length < 10) {
    $(txtMobile).css("border-color", "red");
    return false;
}
$(txtMobile).css("border-color", "")
return true; }

function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n) }

答案 9 :(得分:0)

var message = document.getElementById('contact-error');
$('#contact').focusout(function(){
	if(!$(this).val().match('[0-9]{10}'))  {
		$('#contact-error').addClass('contact-message');
        message.innerHTML = "required 10 digits, match requested format!";
    }else {
    	$('#contact-error').removeClass('contact-message');
    	message.innerHTML = "";
    }
})
.contact-message {
    display: block;
    margin-bottom: 20px;
    color: #cc0033;
}
<input type="text" id="contact" required>
<span id="contact-error"></span>

答案 10 :(得分:0)

$().ready(function () {

    $.validator.addMethod(
            "tendigits",
            function (value, element) {
                if (value == "")
                    return false;
                return value.match(/^\d{10}$/);
            },
            "Please enter 10 digits Contact # (No spaces or dash)"
            );

    $('#frm_registration').validate({
        rules: {
            phone: "tendigits"
        },
        messages: {
            phone: "Please enter 10 digits Contact # (No spaces or dash)",

        }

    });

})