jQuery Example of Regular Expression Based Form Validation Feature

  • 2021-07-13 04:16:28
  • OfStack

This paper illustrates the form validation function based on regular expressions in jQuery. Share it for your reference, as follows:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<script type="text/javascript" language="javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript" language="javascript" >
function validata(){
  if($("#username").val()==""){
  alert(" Please enter a name ");
  return false;
  }
  if($("#password").val()==""){
  alert(" Please enter your password ");
  return false;
  }
  if($("#telephone").val()==""){
  alert(" Please enter a phone number ");
  }
  if($("#email").val()==""){
  $("#email").val("shuangping@163.com");
  }
}
function isInteger(obj){
  reg=/^[-+]?\d+$/;
  if(!reg.test(obj)){
  $("#test").html("<b>Please input correct figures</b>");
  }else{
  $("#test").html("");
  }
}
function isEmail(obj){
  reg=/^\w{3,}@\w+(\.\w+)+$/;
  if(!reg.test(obj)){
  $("#test").html("<b> Please enter the correct email address </b>");
  }else{
  $("#test").html("");
  }
}
function isString(obj){
  reg=/^[a-z,A-Z]+$/;
  if(!reg.test(obj)){
  $("#test").html("<b> Only characters can be entered </b>");
  }else{
  $("#test").html("");
  }
}
function isTelephone(obj){
  reg=/^(\d{3,4}\-)?[1-9]\d{6,7}$/;
  if(!reg.test(obj)){
  $("#test").html("<b> Please enter the correct telephone number! </b>");
  }else{
  $("#test").html("");
  }
}
function isMobile(obj){
  reg=/^(\+\d{2,3}\-)?\d{11}$/;
  if(!reg.test(obj)){
  $("#test").html(" Please enter the correct mobile phone ");
  }else{
  $("#test").html("");
  }
}
function isUri(obj){
  reg=/^http:\/\/[a-zA-Z0-9]+\.[a-zA-Z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
  if(!reg.test(obj)){
  $("#test").html($("#uri").val()+" Please enter the correct inernet Address ");
  }else{
  $("#test").html("");
  }
}
//document Load finished executing 
$(document).ready(function() {
// do something here
// Interlaced color change function 
$("p").each(function(i){
  this.style.color=['red','green','blue','black'][i%2]
  });
//eq(2) Get $("p") The first of the set 3 Elements 
$("p").eq(2).click(function(){$("#display").css("color","blue")});
// All test In p Styles are attached "over" . 
$("#test>p").addClass("over");
//test The last of the 1 A p Style attached "out" . 
$("#test p:last").addClass("out");
// Choosing the same-level elements has not been understood yet 
//$('#faq').find('dd').hide().end().find('dt').click(function()
// Select Parent Element 
$("a").hover(
   function(){$(this).parents("p").addClass("out")},
   function(){$(this).parents("p").removeClass("out")})
//hover Mouse hover effect, toggle Switch the function to be called every time you click  ,
//trigger(eventtype):  In every 1 Some kind of event is triggered on the matching elements ,
//bind(eventtype,fn) , unbind(eventtype):  Event binding and unbinding from each 1 Events that remove bindings from (add) a matching element. 
// Continuous writing of method 
$("#display").hover(function(){
  $(this).addClass("over");
  },function(){
   $(this).removeClass("over");
  })
  .click(function(){alert($("#display").text())});
if($.browser.msie){// Judge the browser, if ie The following functions are performed 
  // Focus 
  $("input[@type=text],textarea,input[@type=password]")
  .focus(function(){$(this).css({background:"white",border:"1px solid blue"})})
  // You can also write it in conjunction like this ,
  //.blur(function(){$(this).css({background:"white",border:"1px solid black"})})
  // Lose focus 
  //css Styles are available through the addClass() To add 
  $("input[@type=text],textarea,input[@type=password]")
  .blur(function(){$(this).css({background:"white",border:"1px solid black"});});
}
});
</script>
<style type="text/css">
.over{
font-size:large;
font-style:italic;
}
.out{
font-size:small;
}
</style>
</head>
<body >
<div id="display">demo</div>
<div id="test">
<p>adfa<a>dfasfa</a>sdfasdf</p>
<p>adfadfasfasdfasdf</p>
<p>adfadfasfasdfasdf</p>
<p>adfadfasfasdfasdf</p>
</div>
<form id="theForm">
isString<div><input type="text" id="username" onblur="isString(this.value)"/></div>
isInteger<div><input type="text" id="password" onblur="isInteger(this.value)"/></div>
isTelephone<div><input type="text" id="telephone" onblur="isTelephone(this.value)"/></div>
isMobile<div><input type="text" id="mobile" onblur="isMobile(this.value)"/></div>
isEmail<div><input type="text" id="email" onblur="isEmail(this.value)"/></div>
isUri<div><input type="text" id="uri" onblur="isUri(this.value)"/></div>
<div><input type="button" value="Validata" onclick="return validata();" /></div>
</form>
</body>
</html>

Attachment: Commonly used js verification functions:

Site Home Form js:


function checkVaild()
{
  var User=$("#Mobile").val();
  var reg=/^(\+\d{2,3}\-)?\d{11}$/;
  if (User=="")
  {
   alert(" Mobile phone number cannot be empty ") ;
   return false;
  }
  if(!reg.test(User)){
  alert(" Incorrect entry of mobile phone number ") ;
  return false ;
  }
  return true ;
}

Filtering Special Characters in Regular Expressions;


function doValidate(value)
{
  vkeyWords=/^[^`~!@#$%^&*()+=|\\\][\]\{\}:;'\,.<>/?]{1}[^`~!@$%^&()+=|\\\][\]\{\}:;'\,.<>?]{0,19}$/;
  if(value==null || value=="")
  {
  alert(" Please enter the correct query parameters ");
  return false;
  }
  if(!vkeyWords.test(value))
  {
  alert(" You have entered incorrect query parameters , Please re-enter !");
  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

For more information about jQuery, please see the topics on this site: "jQuery Regular Expression Usage Summary", "jQuery String Operation Skills Summary", "jQuery Operation xml Skills Summary", "jQuery Extension Skills Summary", "jquery Selector Usage Summary" and "jQuery Common Plugins and Usage Summary"

I hope this article is helpful to everyone's jQuery programming.


Related articles: