Regular expression matching URL, phone number, mobile phone, and email code example display

巴扎黑
Release: 2023-03-16 10:22:01
Original
1450 people have browsed it

This article introduces the method of regular expression matching (URL, phone, mobile phone, email) through example code. It is very good and has reference value. Friends who need it can refer to it

Regular Expression Formula, also known as regular expression. (English: Regular Expression, often abbreviated as regex, regexp or RE in code), a concept in computer science. Regular tables are usually used to retrieve and replace text that matches a certain pattern (rule). Let’s introduce the example code of regular expression matching (URL, phone, mobile phone, email) through example code. Let’s take a look!

Without further ado, I will post the code directly for you. The specific code is as follows:


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>正则验证</title>
</head>
<body>
<input type="text" name="" id="text">
<input type="button" name="" id="btn" value="点击">
<span></span>
</body>
<script type="text/javascript">
var text = document.getElementById(&#39;text&#39;);
var btn = document.getElementById(&#39;btn&#39;);
//1、url示例:
//https://www.baidu.com/s?wd=%E5%88%98%E5%BE%B7%E5%8D%8E&rsv_spt=1&rsv_iqid=0x9601edc200017402&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&rqlang=cn&tn=baiduhome_pg&rsv_enter=1&oq=javascript%2520%25E9%259A%25BE%25E9%25A2%2598&inputT=1256&rsv_t=7da5GmMhTie86h8qaOiaV047P9TkJunMjrkmK%2BNLA%2FuJs3bMG%2Bj52w%2F6IxHge5MB%2B%2B%2Fw&rsv_pq=c270d50b00019cae&rsv_sug3=21&rsv_sug1=19&rsv_sug7=100&rsv_sug2=0&rsv_sug4=1818
//2、电话示例:
//13800138000
//+ 8610 - 59926666
//+ (1)533- 222 - 334
//010 - 59926666
//037132376865
//3、电子邮件示例:
//Handsome.W@abc.com
//_hello_world@163.com
//345@mail.some_domain_name.com.uk
// var regexp=/^([0-9a-zA-Z_.-])+@([0-9a-zA-Z_-])+(\.([a-zA-Z_-])+)+$/;//邮箱
// var regexp=/(https?.*?\.(:?cn\b|com\b|net\b|org\b|gov\b)(?!\.))/;//URL
// var regexp=/\+?\d{3,4}-?\d{7,8}/; // 010-59926666 8610-59926666 037132376865
// var regexp =/\+\(\d\)(\d{3}-){2}\d{3}/;//+(1)533-222-334
// var regexp=/(1[34578]\d{9})|(\+?\d{3,4}-?\d{7,8})|(\+\(\d\)(\d{3}-){2}\d{3})/;//电话号码 13800138000 010-59926666 8610-59926666 037132376865 +(1)533-222-334
var regexp=/(([0-9a-zA-Z_.-])+@([0-9a-zA-Z_-])+(\.([a-zA-Z_-])+)+)|((https?.*?\.(:?cn\b|com\b|net\b|org\b|gov\b)(?!\.)))|(1[34578]\d{9})|(\+?\d{3,4}-?\d{7,8})|(\+\(\d\)(\d{3}-){2}\d{3})/;
btn.onclick = function(){
if(regexp.test(text.value)){
alert(&#39;验证通过&#39;);
text.value="";
}else{
alert(&#39;验证没通过&#39;);
}
}
</script>
</html>
Copy after login

The above is the detailed content of Regular expression matching URL, phone number, mobile phone, and email code example display. 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