validate a URL with Javascript

Marc0

Well-known member
Registered
Joined
Jun 6, 2012
Messages
890
Points
28
The input can begin with either the text "http" or "file", so the two are grouped together with a | to show that either one or the other value is acceptable. Whether the user is getting the image off of their hard drive or off the Web, the next characters have to be "://", so that's checked for next. Note that each of the forward slashes must be escaped individually (that's what the two instances of \/ are, escaped forward slashes), because forward slashes are regular expression special characters.

After that, nearly anything goes, so \S+ is used to signify that one or more nonwhite space characters follow. Then there's another required forward slash (again escaped) to separate the domain from the file name, and then another \S+ to handle the file name.

The file name needs to end with a period and then either "gif" or "jpg". The period is escaped, and the two suffixes are grouped together to test for either.

Code:
var re = /^(file|http):\/\/\S+\/\S+ \.(gif|jpg)$/i;
After the regular expression, the modifier i is used, to allow the user input to be either upper- or lowercase. This modifier tells the regular expression not to be case-sensitive.
 

Marc0

Well-known member
Registered
Joined
Jun 6, 2012
Messages
890
Points
28
good tuts. I like JS it's client scripting and useful for my work
 

avinav.rathore

New member
Registered
Joined
Aug 23, 2013
Messages
5
Points
0
I have used that code & that's excellently working. I have checked that that's working perfectly.
 
Older Threads
Replies
1
Views
2,467
Replies
0
Views
2,342
Replies
0
Views
2,286
Replies
0
Views
2,542
Replies
2
Views
3,133
Newer Threads
Replies
3
Views
5,053
Replies
0
Views
3,837
Replies
0
Views
2,542
Replies
4
Views
3,500
Latest Threads
Replies
0
Views
115
Replies
0
Views
65
Replies
0
Views
57
Replies
1
Views
87
Recommended Threads

Latest postsNew threads

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top