View Full Version : validate a form field

12-08-2006, 11:12 PM
I have a form field that is for a url.

I have the value of the field http:// so that the users can add in www.site.com

How do I validate:

1) there is a website entered
2) that it begins with http:// (in case they tabbed and deleted this part and only have www.site.com or just site.com)

Thanks in advance.

Philip M
12-09-2006, 08:38 AM
if (/^http:\/\/www\./i.test(lstr.value)) {
Alert ("OK");

Tests that the field (lstr) starts with http://www.

Obviously you cannot check that the URL entered is a valid one.

Please don't use the gruesome expression "Thanks in advance". It is rude, and suggests that you take other's help for granted.

12-09-2006, 04:41 PM
I was thinking about this lately as I'm working on validation stuff as well.

I need it for a guestbook, I want to automatically make the users name a link to his/her site. It should however work with all manners I think users are likely to enter their website. These include (but are probably not limmited to :D ):


I think the solution above only detects the first option. I haven't really worked on it yet, so I don't have anything better, I'd just like to know if there are any known fool-proof methods already out there.

Philip M
12-09-2006, 04:56 PM
Anyone silly enough to enter his web URL into a guest book needs to see a doctor right away. There is enough spam about already.

12-09-2006, 08:24 PM
Hardly a helpfull reply but it is a good point, I didn't thought of it before.

However, anywhere I list my own email on my site I use a very basic script to split it up and put it back together at the clients machine. Works well, I can...

Hey wait a min... url and spam? Since when are spambots looking for urls, what use are those?

Anyway, in the CG scene a guestbook is very common, I left many a message with no trouble at all. Regardless, people are free to choose not to leave their site.