客户端验证 [英] client side validation
问题描述
我想通过客户端验证手机号码。我已经验证它是移动设备,不应该从国家代码开始,它应该从9,8或7开始。现在我想验证它,以便它不应少于12位数,包括国家代码。我怎么能实现这个目标?任何人都可以建议我一些示例代码?
我的代码如下:
Hi,
I want to validate a mobile no through client side. I have validated it as mobile no should start with country code and it should start with 9,8 or 7. Now i want to validate it so that it should not less than 12 digit including country code. how can i achieve this? can any one suggest me some sample code?
my code looks like this:
var txtPhone = document.getElementById(phoneID); //validating user phone no
if (txtPhone.value != "") {
var y = txtPhone.value;
if (isNaN(y) || y.indexOf(" ") != -1) {
alert("Invalid Mobile No.");
// return false;
}
if (y.length > 12 || y.length < 12) {
alert("Mobile No. should be entered with country code");
// 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 ");
// return false;
}
}
推荐答案
1。利用HTML5验证
1. Take an advantage of HTML5 validation
<input type="tel" name="phone" required=""></input>
http://diveintohtml5.info/forms。 html [ ^ ]
2. U.不支持的后备
http:// afarkas.github.com/webshim/demos/index.html [ ^ ]或类似的
3.另外还要进行服务器端验证。您使用的语言不太清楚,但任何服务器端语言都有一些验证库。
http://diveintohtml5.info/forms.html[^]
2. Use the fallback where not supported
http://afarkas.github.com/webshim/demos/index.html[^] or similar
3. Always to server side validation in addition. It is not quite clear what language you use, but any server side language has some validation libraries.
这篇关于客户端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!