验证 javascript 中的 phone 数字
Validating a phone number in javascript
我希望能够接受用户输入其 phone 号码的任何方式。我需要帮助来包含可以验证数字长度、括号、连字符并处理空格的正则表达式。
您可以使用此正则表达式。我不太擅长正则表达式,但这可能适用于你的情况。
0\d{2}-\d{7}
/* start match with 0
check for 2 additional digits
check for hyphen
check for 7 additional digits after hyphen
*/
我也不擅长创建正则表达式;但是,您始终可以这样做:
var str = document.getElementById('myInput').value,
numberOnly = str.replace(/-/g, ''),
errors = [], i;
if (isNaN(numberOnly)) {
errors.push('You must use numbers!');
} else if (str.split('-')[0].length !== 3 || str.split('-')[1] !== 7 || numberOnly > 10) {
errors.push('Invalid Format!');
} else {
console.log(numberOnly + ' is ok!');
}
if (errors) {
for (i = 0; i < errors.length; i++) {
console.log(i + '. ' + errors[i]);
}
}
它只是测试提交的字符串的每个部分。
首先它会检查(在我们删除连字符之后)提交的值实际上是一个数字。
其次,它将字符串分成两半,检查字符串的开头是否有 3 个字符,然后检查字符串的结尾是否有 7 个字符;最后,它会测试数字是否太大...等等,您甚至可以检查它是否太小。
如果你想出了一个合适的正则表达式,你可以使用 switch 语句来捕获错误(如果有的话)。
我认为其中一个可能看起来像 [0-9]{3}(-)[0-9]{7} 或类似的东西,哈哈。
-
我使用 PHP 已经有一段时间了,所以我忘记了 "length" returns 是一个计数,还是一个字符的实际字节大小,例如"é" 是 2 个字节。
编辑:
要检查字符串的第一个字符是否为“0”,您可以随时这样做:
if (str.length > 0 && str.charAt(0) != 0) { console.log('error'); }
<script>
function Myfuc() {
var x = document.forms[0]["mobile"].value;
var z = document.forms[0]["mobile1"].value;
if(x == null || x == '')
{
alert("Field canot be empty");
return false;
}
if(x[0]!=0) // Starting with zero
{
alert("Mobile number should start with Zero");
return false;
}
var y=isNaN(x); // Checking numerals in first text box
if(y == true)
{
alert("Integers only accepted");
return false;
}
var z1=isNaN(z); // Checking numerals in first text box
if(z1 == true)
{
alert("Integers only accepted");
return false;
}
}
</script>
<form onsubmit="Myfuc()">
Mobile : <input type="text" id="mobile" name="mobile" style="width:40px;" maxlength=3> -
<input type="text" name="mobile1" maxlength=7>
<input type="submit" value="Click" name="sub" >
</form>
你问的都在这里!!!,花了 1 小时来学习这个和 imma 初学者,不要担心代码运行良好:-)
我希望能够接受用户输入其 phone 号码的任何方式。我需要帮助来包含可以验证数字长度、括号、连字符并处理空格的正则表达式。
您可以使用此正则表达式。我不太擅长正则表达式,但这可能适用于你的情况。
0\d{2}-\d{7}
/* start match with 0
check for 2 additional digits
check for hyphen
check for 7 additional digits after hyphen
*/
我也不擅长创建正则表达式;但是,您始终可以这样做:
var str = document.getElementById('myInput').value,
numberOnly = str.replace(/-/g, ''),
errors = [], i;
if (isNaN(numberOnly)) {
errors.push('You must use numbers!');
} else if (str.split('-')[0].length !== 3 || str.split('-')[1] !== 7 || numberOnly > 10) {
errors.push('Invalid Format!');
} else {
console.log(numberOnly + ' is ok!');
}
if (errors) {
for (i = 0; i < errors.length; i++) {
console.log(i + '. ' + errors[i]);
}
}
它只是测试提交的字符串的每个部分。
首先它会检查(在我们删除连字符之后)提交的值实际上是一个数字。
其次,它将字符串分成两半,检查字符串的开头是否有 3 个字符,然后检查字符串的结尾是否有 7 个字符;最后,它会测试数字是否太大...等等,您甚至可以检查它是否太小。
如果你想出了一个合适的正则表达式,你可以使用 switch 语句来捕获错误(如果有的话)。
我认为其中一个可能看起来像 [0-9]{3}(-)[0-9]{7} 或类似的东西,哈哈。
-
我使用 PHP 已经有一段时间了,所以我忘记了 "length" returns 是一个计数,还是一个字符的实际字节大小,例如"é" 是 2 个字节。
编辑:
要检查字符串的第一个字符是否为“0”,您可以随时这样做:
if (str.length > 0 && str.charAt(0) != 0) { console.log('error'); }
<script>
function Myfuc() {
var x = document.forms[0]["mobile"].value;
var z = document.forms[0]["mobile1"].value;
if(x == null || x == '')
{
alert("Field canot be empty");
return false;
}
if(x[0]!=0) // Starting with zero
{
alert("Mobile number should start with Zero");
return false;
}
var y=isNaN(x); // Checking numerals in first text box
if(y == true)
{
alert("Integers only accepted");
return false;
}
var z1=isNaN(z); // Checking numerals in first text box
if(z1 == true)
{
alert("Integers only accepted");
return false;
}
}
</script>
<form onsubmit="Myfuc()">
Mobile : <input type="text" id="mobile" name="mobile" style="width:40px;" maxlength=3> -
<input type="text" name="mobile1" maxlength=7>
<input type="submit" value="Click" name="sub" >
</form>
你问的都在这里!!!,花了 1 小时来学习这个和 imma 初学者,不要担心代码运行良好:-)