Use JavaScript Regular expressions to verify that the URL format is correct __ regular expressions

Source: Internet
Author: User

Using JavaScript Regular expressions to verify the URL format is correct, a lot of ways, this is only a way to achieve.
The code is as follows:

<script type= "Text/javascript" > 
	function Isurl (str) { 
		var regurl =/(http\:\/\/)? [\w.] +) (\/[\w-\.\/\?%&=]*)/gi; 
		var result = Str.match (Regurl); 
		if (result!=null) {alert ("Yes")} 
		else {alert ("no!") 
}} </script> 

<script type= "text/javascript" > 
	function Isurl (str) { 
		var regurl =/(http\:\/\/)? [\w.] +) (\/[\w-\.\/\?%&=]*)/gi; 
		var result = Str.match (Regurl); 
		if (result!=null) {alert ("Yes")} 
		Else{alert ("no!") 
}} </script>

Call Method:
<form name= "Bao" > 
	<input type= "text" name= "TXT" > 
	<input type=button value= "Testurl" oncli ck= "Isurl (document.bao.txt.value)" > 
</FORM>

Explain:
(http\:\/\/)? Represents a match http://0 or 1 times,
([\w.] +) Matching www.sian.com
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
<script>
    	function Checkadd () {
    		var websitename=$ ("#websiteName"). Val () | | "";
    		var url=$ ("#url"). Val () | | ""; 
    		if (websitename== "") {
    			alert ("Please enter URL name");
    			return false;
    		}  
    		var reg=/^ ([hh][tt]{2}[pp]:\/\/|[ hh][tt]{2}[pp][ss]:\/\/) (([a-za-z0-9-~]+) \.) + ([a-za-z0-9-~\/]) +$/; 
    		if (url== "") {
    			alert ("Please enter URL");
    			return false;
    		}  
    		if (!reg.test (URL)) { 
    		 	alert ("Incorrect URL format");
    		 	return false;
    		}
    		 
    	}
    </script>


Call

<s:submit value= "Add" onclick= "return Checkadd ()" ></s:submit>


/////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////

Another one:

http://blog.csdn.net/vipxiaotian/article/details/1774686

////////////////////////////////////////////////////////////////////

function Checkurl (str) {
    var regurl = new RegExp ();
    Regurl.compile ("^[a-za-z]+://[a-za-z0-9-_]+\\.[ a-za-z0-9-_%&\?\/.=]+$ ");
    if (! Regurl.test (str)) {return
        false;
    }
    return true;
}


//////////////////////////////////////////////////////////////////////

More stringent testing methods

Http://www.phpernote.com/javascript-function/337.html

Related Article

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.