Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expression to validate website URL

Status
Not open for further replies.

RakhiKalra

Programmer
Jun 6, 2003
41
IN
Hi,

in one of my web form i need to validate a text box in which URL of site is entered.I need a regular expression for it.
the URL can not contain ftp ..it can contain http and https.
please help.



Rakhi Kalra
 

Why use a regular expression for that? Just do a simple string test (indexOf) for "http" being at position 0 in the string, surely?

Hope this helps,
Dan
 
This is a simple regex to do exactly what dan suggested, if you really must use regular expressions.
Code:
<HTML>
<HEAD>
<TITLE>Test</TITLE>
<SCRIPT LANGUAGE=JavaScript>

function regexpCheck(str) {
   if ((/^((http)|(https))(:\/\/)/).test(str)) {
      alert ("Legal website address");
   }
   else {
      alert ("String entered was not a website.");
   }
   blahForm.blahText.value = '';
   blahForm.blahText.focus();
}

</SCRIPT>

<BODY>
<FORM NAME="blahForm">
<INPUT TYPE=TEXT NAME=blahText><br>
<INPUT TYPE=BUTTON VALUE='URL Test' NAME=blahButton ONCLICK='javascript:regexpCheck(blahForm.blahText.value);'>
</FORM>
</BODY>
</HTML>

-kaht

banghead.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top