javascript - How to set mobile number validation on my jsp page? - Stack Overflow

i am using this script for validation cananybody help mewhere i am wrong. i am using this foe mobil

i am using this script for validation can anybody help me where i am wrong. i am using this foe mobile number validation.when i run this code with jquery it is not working.

    function mobilenumber() {
          if(document.getElementById('mobnum').value != ""){

       var y = document.getElementById('mobnum').value;
       if(isNaN(y)||y.indexOf(" ")!=-1)
       {
          alert("Invalid Mobile No.");
          document.getElementById('mobnum').focus();
          return false;
       }

       if (y.length>10 || y.length<10)
       {
            alert("Mobile No. should be 10 digit");
            document.getElementById('mobnum').focus();
            return false;
       }
       if (!(y.charAt(0)=="9" || y.charAt(0)=="8" || y.charAt(0)=="7"))
       {
            alert("Mobile No. should start with 9 ,8 or 7 ");
            document.getElementById('mobnum').focus();
            return false
       }

    }
    }

 <input type="submit" value="INSERT"class="btn"onclick="submitForm();">

jquery is

$(document).ready(function(){
function submitForm(){
    if(mobilenumber()){
        $('#form1').submit(function(){

        });

        }
        else{
            alert("Please Input Correct Mobile Numbers");
        }

}
});   

i am using this script for validation can anybody help me where i am wrong. i am using this foe mobile number validation.when i run this code with jquery it is not working.

    function mobilenumber() {
          if(document.getElementById('mobnum').value != ""){

       var y = document.getElementById('mobnum').value;
       if(isNaN(y)||y.indexOf(" ")!=-1)
       {
          alert("Invalid Mobile No.");
          document.getElementById('mobnum').focus();
          return false;
       }

       if (y.length>10 || y.length<10)
       {
            alert("Mobile No. should be 10 digit");
            document.getElementById('mobnum').focus();
            return false;
       }
       if (!(y.charAt(0)=="9" || y.charAt(0)=="8" || y.charAt(0)=="7"))
       {
            alert("Mobile No. should start with 9 ,8 or 7 ");
            document.getElementById('mobnum').focus();
            return false
       }

    }
    }

 <input type="submit" value="INSERT"class="btn"onclick="submitForm();">

jquery is

$(document).ready(function(){
function submitForm(){
    if(mobilenumber()){
        $('#form1').submit(function(){

        });

        }
        else{
            alert("Please Input Correct Mobile Numbers");
        }

}
});   
Share Improve this question edited Jan 2, 2014 at 6:43 Shakti asked Jan 2, 2014 at 5:26 ShaktiShakti 892 gold badges3 silver badges10 bronze badges 2
  • where are you from i mean for which country mobile number you want validation??? – The Hungry Dictator Commented Jan 2, 2014 at 5:30
  • indaia is a country where i am using validation – Shakti Commented Jan 2, 2014 at 5:49
Add a ment  | 

4 Answers 4

Reset to default 1

Use HTML5 Pattern

<input type="number" pattern=".{10}" title="Enter Valid Mob No" required>
 var mobile = document.getElementById("mobnum").value;
 var numericmob = isNumber(mobile );
      if(!numericmob)
      {
          alert("Enter only positive numbers into the Contact Number field.");      
          return false;
      }
      if(mobile.length!=10)
      {
          alert("Enter 10 digits Contact Number.");
          return false;
      }

// write function that checks element is number or not

function isNumber(elem)
{    
    var re = /^[0-9]+$/; 
    str = elem.toString();
    if (!str.match(re)) 
    {               
        return false;
    }
    return true;
}

Advantage of this function is you can use it for checking any numeric field on your form, such as id, amount etc.

Use jQUery .submit() function to submit the form after validation is true

<form id="myForm" action="abc.php" method="post">

<!-- Your Form -->

<button onclick="submitForm();">Submit</button>
</form>

now to check if form data is valid here

function submitForm(){
    if(mobilenumber()){
        $('#myForm').submit();
        }
        else{
            alert("Please Input Correct Mobile Numbers");
        }
}

EDIT: Use the @Aniket's isNumber function to check length and digit in mobile number field. Which is more generic.

if(document.getElementById('mobile_number').value != ""){

   var y = document.getElementById('mobile_number').value;
   if(isNaN(y)||y.indexOf(" ")!=-1)
   {
      alert("Invalid Mobile No.");
      document.getElementById('mobile_number').focus();
      return false;
   }

   if (y.length>10 || y.length<10)
   {
        alert("Mobile No. should be 10 digit");
        document.getElementById('mobile_number').focus();
        return false;
   }
   if (!(y.charAt(0)=="9" || y.charAt(0)=="8" || y.charAt(0)=="7"))
   {
        alert("Mobile No. should start with 9 ,8 or 7 ");
        document.getElementById('mobile_number').focus();
        return false
   }

}

try this... this will be needful for you... and if you are looking for ragex pattern then use this....

^([0|\+[0-9]{1,5})?([7-9][0-9]{9})$

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744994790a4605123.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信