Recent lucram la un site si aveam nevoie de o validare din javascript pentru un URL. Am gasit ca exista mai multe posibilitati de a face acest lucru.

Exemplu 1:

[code]
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!–
function Validate(form) {
var v = new RegExp();
v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
if (!v.test(form["URL"].value)) {
alert("You must supply a valid URL.");
return false;
}
}
//–>
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="MyForm" ACTION="MyFile.php" METHOD="POST" onSubmit="return Validate(this);">
<INPUT TYPE="TEXT" NAME="URL" VALUE="https://">
<INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="SUBMIT">
</FORM>
</BODY>
</HTML>
[/code]

Sursa

Exemplu 2:

[javascript]
function checkURL(value) {
var urlregex = new RegExp("^(https:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}[0-9A-Za-z\.\-]*\.[0-9A-Za-z\.\-]*$");
if(urlregex.test(value))
{
return(true);
}
return(false);
}
[/javascript]

Sursa

Exemplu 3:

[javascript]
function isUrl(s) {
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
return regexp.test(s);
}
[/javascript]

Sursa

Voi ce variante folositi pentru validarea unei expresii care trimite spre un site?

Citeste si:   jQuery: executa o functie dupa incarcarea paginii

Spune si tu parerea...