javascript 单元格编号验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18950771/
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
javascript cell number validation
提问by sapan
I want to validate cell number using JavaScript.
我想使用JavaScript验证单元格编号。
Here is my code.
这是我的代码。
if(number.value == "") {
window.alert("Error: Cell number must not be null.");
number.focus();
return false;
}
if(number.length != 10) {
window.alert("Phone number must be 10 digits.");
number.focus();
return false;
}
Here is the issue, when I submit the form with out entering the phone number, it is showing the error cell number must not be null. it works fine.
这是问题,当我提交表单而不输入电话号码时,它显示错误的手机号码不能为空。它工作正常。
When I submit the form with cell number less than 10 digits, it is showing phone number must be 10 digits. It is also fine.
当我提交手机号码少于 10 位的表格时,显示电话号码必须为 10 位。这也很好。
The problem is when I submit the form with 10 digits, then also it is showing the error phone number must be 10 digits.
问题是当我提交 10 位数字的表单时,它也显示错误电话号码必须是 10 位数字。
Please help me. Thank You.
请帮我。谢谢。
And also need the validation code for only digits for cell number.
并且还需要仅用于手机号码数字的验证码。
回答by Doorknob
If number
is your form element, then its length will be undefined
since elements don't have length. You want
如果number
是您的表单元素,那么它的长度将是undefined
因为元素没有长度。你要
if (number.value.length != 10) { ... }
An easier way to do all the validation at once, though, would be with a regex:
但是,同时进行所有验证的更简单方法是使用正则表达式:
var val = number.value
if (/^\d{10}$/.test(val)) {
// value is ok, use it
} else {
alert("Invalid number; must be ten digits")
number.focus()
return false
}
\d
means "digit," and {10}
means "ten times." The ^
and $
anchor it to the start and end, so something like asdf1234567890asdf
does not match.
\d
表示“数字”,{10}
表示“十次”。在^
与$
锚它开始和结束,所以像asdf1234567890asdf
不匹配。
回答by Shankar M
function IsMobileNumber(txtMobId) {
var mob = /^[1-9]{1}[0-9]{9}$/;
var txtMobile = document.getElementById(txtMobId);
if (mob.test(txtMobile.value) == false) {
alert("Please enter valid mobile number.");
txtMobile.focus();
return false;
}
return true;
}
Calling Validation Mobile Number Function HTML Code -
呼叫验证手机号码功能 HTML 代码 -
回答by Bharath Kumaar
Mobile number Validation using Java Script, This link will provide demoand more information.
使用 Java Script 进行手机号码验证,此链接将提供演示和更多信息。
function isNumber(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
alert("Please enter only Numbers.");
return false;
}
return true;
}
function ValidateNo() {
var phoneNo = document.getElementById('txtPhoneNo');
if (phoneNo.value == "" || phoneNo.value == null) {
alert("Please enter your Mobile No.");
return false;
}
if (phoneNo.value.length < 10 || phoneNo.value.length > 10) {
alert("Mobile No. is not valid, Please Enter 10 Digit Mobile No.");
return false;
}
alert("Success ");
return true;
}
<input id="txtPhoneNo" type="text" onkeypress="return isNumber(event)" />
<input type="button" value="Submit" onclick="ValidateNo();">
回答by Gopikrishna Mallik
This function check the special chars
on key press and eliminates the value if it is not a number
此功能检查特殊chars
键按下并消除该值,如果它不是number
function mobilevalid(id) {
var feild = document.getElementById(id);
if (isNaN(feild.value) == false) {
if (feild.value.length == 1) {
if (feild.value < 7) {
feild.value = "";
}
} else if (feild.value.length > 10) {
feild.value = feild.value.substr(0, feild.value.length - 1);
}
if (feild.value.charAt(0) < 7) {
feild.value = "";
}
} else {
feild.value = "";
}
}
回答by Suraj Khan
If you type:
如果您键入:
if { number.value.length!= 10}...
It will sure work because the value is the quantity which will be driven from the object.
它肯定会起作用,因为该值是将从对象驱动的数量。
回答by Raju Singh
<script type="text/javascript">
function MobileNoValidation()
{
var phno=/^\d{10}$/
if(textMobileNo.value=="")
{
alert("Mobile No Should Not Be Empty");
}
else if(!textMobileNo.value.match(phno))
{
alert("Mobile no must be ten digit");
}
else
{
alert("valid Mobile No");
}
}
</script>
回答by Saurabh Joshi
I used the follow code.
我使用了以下代码。
var mobileNumber=parseInt(no)
if(!mobileNumber || mobileNumber.toString().length!=10){
Alert("Please provide 10 Digit numeric value")
}
If the mobile number is not a number, it will give NaN
value.
如果手机号码不是数字,它将赋予NaN
价值。
回答by user12840125
<script>
function validate() {
var phone=document.getElementById("phone").value;
if(isNaN(phone))
{
alert("please enter digits only");
}
else if(phone.length!=10)
{
alert("invalid mobile number");
}
else
{
confirm("hello your mobile number is" +" "+phone);
}
</script>
回答by Kumar Kirti
Verify this code : It works on change of phone number field in ms crm 2016 form .
验证此代码:它适用于更改 ms crm 2016 表单中的电话号码字段。
function validatePhoneNumber() {
var mob = Xrm.Page.getAttribute("gen_phone").getValue();
var length = mob.length;
if (length < 10 || length > 10) {
alert("Please Enter 10 Digit Number:");
Xrm.Page.getAttribute("gen_phone").setValue(null);
return true;
}
if (mob > 31 && (mob < 48 || mob > 57)) {} else {
alert("Please Enter 10 Digit Number:");
Xrm.Page.getAttribute("gen_phone").setValue(null);
return true;
}
}