简体   繁体   English

我正在尝试使用 javaScript 验证移动设备,但它不起作用

[英]I am trying to validate mobile with javaScript but it not working

I have a mobile and I want to check if the mobile number is less then 10 or greater then 13 then I want to show the message我有一部手机,我想检查手机号码是否小于 10 或大于 13 然后我想显示消息

My code:我的代码:

<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>

In the above code, I validate my code with javascript.在上面的代码中,我使用 javascript 验证了我的代码。

Try this :尝试这个 :

var mob = '03311111111';

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

https://jqueryvalidation.org 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
                  }
              },
          }
      );

You can try the following:您可以尝试以下操作:

$('#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
    }
});

Your final code would be this:你的最终代码是这样的:

 <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>

Or you could use html attributes to do that by using minlength and maxlength on the text input and you may even want to switch the input type from text to tel或者,您可以通过在文本输入上使用minlengthmaxlength来使用 html 属性来执行此操作,您甚至可能希望将输入类型从text切换为tel

<input name="mobile" id="mobile" placeholder="+91" class="form-control tboxs" type="tel" style="font-family: 'JameelKhushkhatLRegular'" minlength='10' maxlength='13'>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试使用JavaScript中的正则表达式验证日期,但无法正常工作? - I am trying to validate a date with a regular expression in JavaScript but it is not working correctly? 我正在尝试验证我的文本字段,而没有javascript中的警报功能,但是它不起作用 - I am trying to validate my textfield without alert function in javascript but it's not working 我正在尝试使用JavaScript控制器中的正则表达式验证字符串? - I am trying to validate a string with a regular expression in the javascript controller? 我试图验证一个简单的HTML表单 - I am trying to validate a simple html form 我正在尝试使用javascript更改链接的颜色并且它不起作用 - I am trying to change the color of link using javascript and its not working 我正在尝试用 Javascript 制作一个计算器。 但它以某种方式不起作用 - I am trying to make a calculator in Javascript. But it is not working somehow 我正在尝试使用 javascript 进行表单验证,但 email 验证不起作用 - I am trying to make form validation with javascript but email validation not working 我正在尝试使用JavaScript验证用户输入。 我想验证用户是否正确输入了他们的课程名称 - I am trying to validate user input using JavaScript. I would like to validate that the user has input their the title of their course correctly 我正在尝试通过使用jqueryValidation引擎插件来验证注册表单,某些验证无法正常工作 - I am trying to validate a signup form by using jqueryValidation engine plugin some validations are not working correctly 尝试验证以便可以使用Javascript - Trying to Validate so I can use Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM