js determines whether the mobile phone number is correct and returns the implementation code

  • 2021-07-12 04:32:35
  • OfStack

js judges whether the mobile phone number is correct and returns the code as follows:


<form method="post" action="login.php" onsubmit="return abc()">
 <input type="text" id="phone" />
 <input type="submit" value=" Login "/> </form>
<script type="text/javascript">
function abc() {
 var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
 if(!myreg.test($("#phone").val()))
 {
  alert(' Please enter a valid mobile phone number! ');
  return false;
 }
 callback();
}
</script>

The above js judgment is 130-139, 150-159 and 180-189. If other segments are needed, add them by yourself

The effect is that when clicking the login button, if the mobile phone number is incorrect, no jump will be made

PS: JS determines whether the mobile phone number is correct (regular expression)


/^13d{9}$/g||/^15[8,9]d{8}$/g

What does it mean to begin with? What does it mean to end with, d matches are any 1-bit digits, The following {9} refers to the number of repetitions of d. The equivalent of dddddddd is a regular expression that matches strings that start with 13 and end with any nine numbers, The/is the delimiter of the regular expression, followed by g, which is a global match for the specified string (if it is not added, only the first one is matched). In fact, the situation with this g and without g is the same here. The test method returns an Boolean value, which indicates whether there is a pattern in the searched string. If there is, it returns true, otherwise it returns false.

g means global, all, that is, all qualified in the text. Without this g, it only matches once.

. test (), which is the preceding one/^...... $/g, is a method of the regular expression.

The following is a complete monitoring code for your reference:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script language="javascript">
function chk()
{
var tel = document.all("text").value;
if(/^13d{9}$/g.test(tel)||(/^15[8,9]d{8}$/g.test(tel)))
  {
   alert(" The mobile phone number is correct ");
   }
else
  {
   alert(" Wrong mobile phone number ");
   }
}
</script>
</HEAD>
<BODY>
<form method="" action="">
 Mobile phone number :<input type="text" name="text">
 <input type="button" value=" Detection " onclick="chk()">
</form>
</BODY>
</HTML>

Verify China fixed telephone number:


((d{3,4})|d{3,4}-|s)?d{8}

Related articles: