Today, I sorted out the url regular example of the url and the email address, and summarized the usage of jquery and js. There is no difference between others. I hope you can help me with this tutorial.
A friend asked me this question today, so I plan to share it with you.
In fact, the implementation is very simple, because the regular expression rules are the same and can be used in PHP, so it can also be used in js.
As long as the regular expression is correctly written, it is half done.
The following code is displayed.
Don't forget to introduce the jquery library.
The Code is as follows: |
Copy code |
Function ismessemailok (messemail ){ Var a =/w + ([-+.] w +) * @ w + ([-.] w + )*. w + ([-.] w + )*/; If (! A. test (messemail )){ Alert ("Warning: Incorrect email format! "); // Here you can write some other operations, such as resetting the form or something. For example, set the input box id to messemail to null. $ ('# Messemail'). val (''); F. messemail. focus (); Return false; } Else { Return true; } } |
Or directly use js
The regular expression is very convenient, "/^ ([a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + ((. [a-zA-Z0-9 _-] {2, 3}) {1, 2}) $/"the correct mailbox format that meets the requirements.
The Code is as follows: |
Copy code |
Function isEmail (str ){ Var reg =/^ ([a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + ((. [a-zA-Z0-9 _-] {2, 3}) {1, 2}) $ /; Return reg. test (str ); } |
Jquery standard network address regular, such as http://www.bKjia. c0m
The Code is as follows: |
Copy code |
Function ismesswebsiteok (messwebsite ){ Var a =/(https ?) : // ([^./] +) ([.]?) ([^./] +) ([.]?) ([^./] +) (/[W -./? % & =] *)? /I; If (! A. test (messwebsite )){ Alert ("Warning: the URL format is incorrect! "); // Here you can write some other operations, such as resetting the form or something. For example, set the input box id to messwebsite to null. $ ('# Messwebsite'). val (''); F. messwebsite. focus (); Return false; } Else { Return true; } } |
If you want to include a port, you can use the following instance.
JavaScript for judging the regular expression of this url! Comprehensive. It verifies the IP address, domain name (domain), ftp, second-level domain name, file in the domain name, domain name plus port! User name and other information, it seems that the author is also looking for online, I deducted from a project code, is the strongest and most comprehensive url verification method I have ever seen! It's so fierce. I posted it here to share it with you first. If I don't remember it later, I will look for it on my blog. URL verification is very frequent.
The Code is as follows: |
Copy code |
Function IsURL (str_url) Var strRegex = "^ (https | http | ftp | rtsp | mms )? ://) + "? ([0-9a-z _!~ * '(). & =+ $ %-] + :)? [0-9a-z _!~ * '(). & =+ $ %-] + @)? "// Ftp user + "([0-9] {1, 3}.) {3} [0-9] {1, 3}" // URL in IP Format-199.194.52.18 + "|" // Allow IP and DOMAIN (DOMAIN Name + "([0-9a-z _!~ * '()-] +.) * "// Domain name-www + "([0-9a-z] [0-9a-z-] {0, 61 })? [0-9a-z]. "// second-level domain + "[A-z] {2, 6})" // first level domain-. com or. museu + "(: [0-9] {1, 4 })? "// Port-: 8 + "((/?) | "// A slash isn' t required if there is no file nam + "(/[0-9a-z _!~ *'().;? : @ & =+ $, % #-] +) + /?) $" Var re = new RegExp (strRegex) // Re. test ( If (re. test (str_url )) Return (true) } Else Return (false) } |