Method of js and jquery Regular Verification of E mail Mobile Phone Number and Postal Code


This paper describes the method of js and jquery to regularly verify e-mail, mobile phone number and postal code.

jQuery code:

// Verify zip code
 $("#postcode").blur(function(){
  // Get Zip Code
  var postcode=$("#postcode").val();
  if(is_postcode(postcode)){
   $("#postcode_info").html("");
  }else{
   $("#postcode_info").html(" The zip code format is incorrect ");
   return false;
  }
 });
 // Verify the mobile phone number
 $("#mobile").blur(function(){
  // Get the mobile phone number , And remove the left and right spaces
  var mobile=$.trim($("#mobile").val());
  if(is_mobile(mobile)){
   $("#mobile_info").html("");
  }else{
   $("#mobile_info").html(" The mobile phone number format is incorrect ");
   return false;
  }
 });
 // Validation email
 $("#email").blur(function(){
  // Get email
  var email=$("#email").val();
  if(is_email(email)){
   $("#email_info").html("");
  }else{
   $("#email_info").html(" The email format is incorrect ");
   return false;
  }
 });
});

js code:

// Order Submission Page - Verify zip code
function is_postcode(postcode) {
 if ( postcode == "") {
  return false;
 } else {
  if (! /^[0-9][0-9]{5}$/.test(postcode)) {
   return false;
  }
 }
 return true;
}
// Order Submission Page - Verify the mobile phone number
function is_mobile(mobile) {
  if( mobile == "") {
  return false;
  } else {
  if( ! /^0{0,1}(13[0-9]|15[0-9]|18[0-9]|14[0-9])[0-9]{8}$/.test(mobile) ) {
  return false;
  }
  return true;
 }
}
// Order Submission Page - Validation email Legitimacy of
function is_email(email) {
 if ( email == "") {
  return false;
 } else {
  if (! /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/.test(email)) {
   return false;
  }
 }
 return true;
}

PS: Here are two very convenient regular expression tools for your reference:

JavaScript Regular Expression Online Test Tool: http://tools.ofstack.com/regex/javascript

Regular expression online generation tool: http://tools.ofstack.com/regex/create\_reg

I hope this article is helpful to everyone’s javascript programming.