...

View Full Version : validate URL and Keywords



marian2oo7
06-01-2009, 09:34 AM
I want to validate an imdb URL and an keywords field who contain minimum 3 words separated by comma.
This script valdate any url, but i want to validate url like this:

http://www.imdb.com/title/tt0329749/

var urlRegex = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;

if(url == "") {
inlineMsg('url','<strong>Error</strong><br />You must enter an URL.',2);
return false;
}
if(!url.match(urlRegex)) {
inlineMsg('url','<strong>Error</strong><br />You have entered an invalid url.',2);
return false;
}
Thx! Sorry for my english.

Philip M
06-01-2009, 11:50 AM
You do not make it clear what you mean by "like this" but the following will validate a suffix title followed by / followed by (any) 2 letters (upper or lower case) and 7 digits followed by /

var urlRegex = /^http:\/\/[A-Z0-9\.-]{3,}\.[A-Z]{3}\/title\/[A-Z]{2}\d{7}\/$/i;

Note the ^ and $ characters which mean "and nothing but". The /i switch means "ignore case".

If www.imdb.com is a constant then you can simplify:-

var urlRegex = /^http:\/\/www\.imdb\.com\/title\/[A-Z]{2}\d{7}\/$/i;

But why not simply request the user to enter a title reference and add all the prefixes to make it an URL automatically?

You second question:-


var x = "ab,bc,cd";
x = x.replace(/[^a-z,]/gi,""); //strip anything but a-z and comma
if (!/(,.+,)/g.test(x)) {
alert ("You must enter at least three words separated by commas");
}


Always go to other people’s funerals, otherwise they won’t come to yours.

marian2oo7
06-01-2009, 01:48 PM
thank you Philip M for replay, the url verification is work greate, but when checking keywords and i press duble click on submit button, is ignore the verification and the form are submited.
If you have another idea please tell me.

Philip M
06-01-2009, 03:13 PM
thank you Philip M for replay, the url verification is work greate, but when checking keywords and i press duble click on submit button, is ignore the verification and the form are submited.
If you have another idea please tell me.

Well, you must wrap it in a function which you call onsubmit = "return check3()"


function check3() {
var x = document.formname.fieldname.value;
x = x.replace(/[^a-z,]/gi,""); //strip anything but a-z and comma
if (!/(.+,.+,.+)/g.test(x)) {
alert ("You must enter at least three words separated by commas");
}
return false;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum