javascript 如何在我的jsp页面上设置手机号码验证?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20876866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set mobile number validation on my jsp page?
提问by Shakti
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.
我正在使用此脚本进行验证,任何人都可以帮助我哪里出错了。我正在使用这个敌人的手机号码验证。当我使用 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();">
jquery is
jQuery 是
$(document).ready(function(){
function submitForm(){
if(mobilenumber()){
$('#form1').submit(function(){
});
}
else{
alert("Please Input Correct Mobile Numbers");
}
}
});
回答by Sridhar R
Use HTML5 Pattern
使用 HTML5 模式
<input type="number" pattern=".{10}" title="Enter Valid Mob No" required>
回答by Aniket Kulkarni
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 numericfield on your form, such as id, amount etc.
此功能的优点是您可以使用它来检查表单上的任何数字字段,例如 ID、金额等。
回答by Aks
Use jQUery .submit() function to submit the form after validation is true
使用jQUEry .submit() 函数在验证为真后提交表单
<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.
编辑:使用@Aniket 的 isNumber 函数检查手机号码字段中的长度和数字。哪个更通用。
回答by The Hungry Dictator
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....
试试这个......这对你来说是必要的......如果你正在寻找 ragex 模式然后使用这个......
^([0|\+[0-9]{1,5})?([7-9][0-9]{9})$