Javascript 如何在我的jsp页面上设置手机号码验证?

Javascript 如何在我的jsp页面上设置手机号码验证?,javascript,jsp,Javascript,Jsp,我正在使用这个脚本进行验证,任何人都可以帮助我在哪里出错。我正在使用此foe手机号码验证。当我使用jquery运行此代码时,它不起作用 function mobilenumber() { if(document.getElementById('mobnum').value != ""){ var y = document.getElementById('mobnum').value; if(isNaN(y)||y.indexOf(" ")

我正在使用这个脚本进行验证,任何人都可以帮助我在哪里出错。我正在使用此foe手机号码验证。当我使用jquery运行此代码时,它不起作用

    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();">
使用HTML5模式

<input type="number" pattern=".{10}" title="Enter Valid Mob No" required>

//写入检查元素是否为数字的函数

function isNumber(elem)
{    
    var re = /^[0-9]+$/; 
    str = elem.toString();
    if (!str.match(re)) 
    {               
        return false;
    }
    return true;
}
此函数的优点是,您可以使用它检查表单上的任何数字字段,如id、金额等。

使用jQUery.submit()函数在验证为真后提交表单

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

<!-- Your Form -->

<button onclick="submitForm();">Submit</button>
</form>
编辑:
使用@Aniket的isNumber函数检查手机号码字段中的长度和数字。哪个更通用。

您来自哪里?我的意思是您希望验证哪个国家的手机号码???印度是我使用验证的国家
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
   }
<form id="myForm" action="abc.php" method="post">

<!-- Your Form -->

<button onclick="submitForm();">Submit</button>
</form>
function submitForm(){
    if(mobilenumber()){
        $('#myForm').submit();
        }
        else{
            alert("Please Input Correct Mobile Numbers");
        }
}
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
   }
^([0|\+[0-9]{1,5})?([7-9][0-9]{9})$