Three JS URL validation function code

Source: Internet
Author: User

<title> Check for url</title>
<script language= "Web Effects" >
function Check () {
str = Form1.txt.value;
str = Str.match (/http://.+/);
if (str = null) {
Alert (' The URL you entered is invalid ');
return false;
}else{
Alert ("The URL you entered is valid");
return true;
}
}
</script>
<body>
<form name= "Form1" onsubmit= "return Check ()" >
<input type= "text" name= "TXT" >
<input type= "Submit" value= "Inspection" >
</form>
</body>

Instance validation code two

The main validation is the regular expression of the format. This is a common method, of course, can be more intelligent to write logical code, or to increase the B/s protocol

function Isurl (str_url) {//Authentication URL
var Strregex = "^ (https tutorial |http|ftp|rtsp|mms)?:/ /)"
+ "? ([0-9a-z_!~* ' (). &=+$%-]+:)? [0-9a-z_!~* ' (). &=+$%-]+@)? " user@ of FTP
+ "([0-9]{1,3}.) {3} [0-9] {1,3} "//IP form of url-199.194.52.184
+ "|"//Allow IP and domain (domains)
+ "([0-9a-z_!~* ' ()-]+.) * "//Domain name-www."
+ "([0-9a-z][0-9a-z-]{0,61})?" [0-9a-z]. " Level two domain name
+ "[a-z]{2,6}]"///domain-. com or. Museum
+ "(: [0-9]{1,4})?"//Port-: 80
+ "((/?)|" A slash isn ' t required if there is no file name
+ "(/[0-9a-z_!~* ' ().;?: @&=+$,%#-]+) +/?" $";
var re = new RegExp (Strregex);
Return Re.test (Str_url);
}


A more advanced URL-Address validation function

It verifies that the situation includes: IP, domain, FTP
Two-level domain name, the domain name of the file, domain name plus port.

<script language= "JavaScript" >
<!--
function Isurl (str_url) {
var Strregex = "^ (HTTPS|HTTP|FTP|RTSP|MMS)?:/ /)"
+ "? ([0-9a-z_!~* ' (). &=+$%-]+:)? [0-9a-z_!~* ' (). &=+$%-]+@)? " user@ of FTP
+ "([0-9]{1,3}.) {3} [0-9] {1,3} "//IP form of url-199.194.52.184
+ "|"//Allow IP and domain (domains)
+ "([0-9a-z_!~* ' ()-]+.) * "//Domain name-www."
+ "([0-9a-z][0-9a-z-]{0,61})?" [0-9a-z]. " Level two domain name
+ "[a-z]{2,6}]"///domain-. com or. Museum
+ "(: [0-9]{1,4})?"//Port-: 80
+ "((/?)|" A slash isn ' t required if there is no file name
+ "(/[0-9a-z_!~* ' ().;?: @&=+$,%#-]+) +/?" $";
var re=new regexp (Strregex);
Re.test ()
if (Re.test (Str_url)) {
return (true);
}else{
return (false);
}
}
-->
</script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.