我正在尝试使用javaScript验证手机,但无法正常工作

时间:2019-12-13 11:14:05

标签: javascript php html

我有一个手机,我想检查手机号码是否小于10或大于13,然后才显示消息

我的代码:

<input name="mobile" id="mobile" placeholder="+91" class="form-control tboxs" type="text" style="font-family: 'JameelKhushkhatLRegular'">

<button style="width: 100%" type="button"  id="submit" value="submit" class="btn-theme-colored btn">SUBMIT <span class="glyphicon glyphicon-send">

        <script>
            $('#submit').click(function(){
       if($('#first_name').val() == '' ){
          alert('Name can not be left blank and atleast 4 char long');
          return false;
        }else if(!$("input[name='redio_gender']:checked").val()){
          alert('Please Select Gender');
          return false;   
        }else if($('#multiple').val( ) == '') {
          alert('Please Select Age');
          return false;
        }else if($('#profession').val( ) == '') {
          alert('Please Select your Profession');
          return false;
        }else if($('#taluka').val( ) == '') {
          alert('Please Select Taluka');
          return false;
        }else if($('#village').val( ) == '') {
          alert('Please Enter village');
          return false;  
        }else if($('#interest').val( ) == '') {
          alert('Please Select Area of Interest');
          return false;   
        }else if($('#masjid').val( ) == '') {
          alert('Please Enter Nearest Masjid');
          return false; 
        }else if($('#mobile').val( ) == '' || parseInt($('#mobile').val() < 10 )  ||  parseInt($('#mobile').val() > 13)) {
          alert('Please Enter Valid Mobile Number');
          return false;  
        }else{  
          $.ajax({
              --
              --
              });
       }
 });
        </script>

在上面的代码中,我使用javascript验证了我的代码。

3 个答案:

答案 0 :(得分:2)

尝试一下:

var mob = '03311111111';

if( mob.length < 10 || mob.length > 13){
    // show error message
}

答案 1 :(得分:2)

https://jqueryvalidation.org

      $.validator.addMethod(
        'phone',
        function (value, element, requiredValue) {
          var phoneRegexp = /^\+380\d{7,10}$/;
          return phoneRegexp.test(value);
        },
      );

      var validator = $('#form_id').validate({
              debug: true,
              errorClass: 'error-class',
              errorElement: 'div',
              rules: {
                  'phone': {
                      required: true,
                      phone: true,
                      minlength: 10,
                      maxlength: 13
                  }
              },
          }
      );

答案 2 :(得分:2)

您可以尝试以下操作:

$('#submit').click(function(){
    var mobile = $('#mobile').val();
    if(mobile.length < 10 || mobile.length > 13) {
        //mobile length is less than 10 or greater than 13, show error message
    }
});

您的最终代码如下:

 <script>
       $('#submit').click(function(){

       var mobile = $('#mobile').val();

       if($('#first_name').val() == '' ){
          alert('Name can not be left blank and at least 4 char long');
          return false;
        } else if(!$("input[name='redio_gender']:checked").val()){
          alert('Please Select Gender');
          return false;   
        } else if($('#multiple').val( ) == '') {
          alert('Please Select Age');
          return false;
        } else if($('#profession').val( ) == '') {
          alert('Please Select your Profession');
          return false;
        } else if($('#taluka').val( ) == '') {
          alert('Please Select Taluka');
          return false;
        } else if($('#village').val( ) == '') {
          alert('Please Enter village');
          return false;  
        } else if($('#interest').val( ) == '') {
          alert('Please Select Area of Interest');
          return false;   
        } else if($('#masjid').val( ) == '') {
          alert('Please Enter Nearest Masjid');
          return false; 
        } else if(mobile.length < 10 || mobile.length > 13) {
          alert('Please Enter Valid Mobile Number');
          return false;  
        } else {  
          $.ajax({
              --
              --
              });
       }
 });
        </script>

或者您可以通过在文本输入上使用minlengthmaxlength来使用html属性,甚至可以将输入类型从text切换为{{1} }

tel