JavaScript Tutorial/Regular Expressions/URL

Материал из Web эксперт
Перейти к: навигация, поиск

Use regular expression to validate the URL

<HTML>
<HEAD>
<SCRIPT language="JavaScript">
<!--
function check_it()
{
     var theurl=document.myForm.t1.value;
     var tomatch= /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/
     if (tomatch.test(theurl)){
         window.alert("URL OK.");
         return true;
     }
     else
     {
         window.alert("URL invalid. Try again.");
         return false;
     }
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM name="myForm" action="#" onSubmit="return check_it();">
Enter URL:<BR>
<INPUT type="text" name="t1">
<BR>
<INPUT type="submit" value="Submit">
</BODY>
</HTML>