Home > Web Front-end > JS Tutorial > body text

Detailed graphic and text explanation of using regular expressions in jQuery

php中世界最好的语言
Release: 2018-03-30 10:58:07
Original
1606 people have browsed it

This time I will bring you a detailed graphic and text explanation of using regular expressions in jQuery. What are the precautions for using regular expressions in jQuery? The following is a practical case. , let’s take a look.

Basic regular expressions

1. Creation of regular expressions

a) var checkNum = /^[A-Za-z0-9]+$/;
b) var re=new RegExp(“["+s1+"]“,”g”);
Copy after login

2. Common rules

a) 用户密码:/^[a-zA-Z][a-zA-Z0-9_]{5,20}$/
b) 邮件:/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/
c) 手机:/^[\d]{5,20}$/
d) 其它常用验证:请百度
Copy after login

3. Method: test

Case

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>jquery ajax</title>
  <script type="text/javascript" src="public/js/jquery-2.2.3.min.js"></script>
</head>
<body>
<form action="">
  <label>用户名:</label><span id="check_username">检测</span>
  <input type="text" id="t_username" placeholder="请输入"/>
  <hr/>
  <label>邮箱:</label><span id="check_email">检测</span>
  <input type="text" id="t_email" placeholder="请输入"/>
  <hr/>
  <label>手机:</label><span id="check_phone">检测</span>
  <input type="text" id="t_phone" placeholder="请输入"/>
  <hr/>
</form>
</body>
<script>
  $(function () {
    // 用户名
    $("#check_username").click(function(){
      var str = $("#t_username").val();
      var ret = /^[a-zA-Z][a-zA-Z0-9_]{5,20}$/;
      if(ret.test(str)){
        alert('ok');
      }else{
        alert('wrong');
      }
    });
    // 邮件
    $("#check_email").click(function(){
      var str = $("#t_email").val();
      var ret = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
      if(ret.test(str)){
        alert('ok');
      }else{
        alert('wrong');
      }
    });
    // 手机
    $("#check_phone").click(function(){
      var str = $("#t_phone").val();
      var ret = /^[\d]{5,20}$/;
      if(ret.test(str)){
        alert('ok');
      }else{
        alert('wrong');
      }
    });
  });
</script>
</html>
Copy after login

Effect Demonstration Picture

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed analysis of matching single characters using regular expressions

Detailed explanation of the use of regular metacharacters

The above is the detailed content of Detailed graphic and text explanation of using regular expressions in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!