Find help, info, instructions, tips, tricks

Tip: Use search box below or this box, labels in the first right sidebar, archive, ctrl+F for this page or sitemap to find topics
1

issue with URL validation MVC razor and JQuery





I am working on MVC Razor. I face an issue to validate URL validation.



I need to do URL validation before inserting in temp DB.



I got a reference of http://www.regxlib.com/Search.aspx?k=&c=2&m=-1&ps=20



and found one nice validation pattern,




(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?



While i work in MVC view, it gives me error so i have change it to




(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@@?^=%&:/~\+#]*[\w\-\@@?^=%&/~\+#])?



i.e.



@@ instead of @



when it runs and if i see the console, i got the original one, so i think it is okay for me to do @@ instead of @



But in console i got an error



enter image description here



can anyone tell me what is wrong with this, so got the error,



"invalid range in character class"



Responses

1 Respones to "issue with URL validation MVC razor and JQuery"

Tips For All said...

Easiest way to fix this: In the javascript validator, remove new RegExp() as well as the quotes, and add slashes at beginning and end, indicating a regex literal. Because your Regex is in a string, "\" is interpreted as a string escape, rather than a regex escape, essentially meaning all the "\" will be removed. You avoid that by not using a string, but a javascript regex literal:

var urlregex = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;


If creating the expression using RegExp(), you'd have to use \\ in every place \ occurs.

As mentioned in the comment, replace two & with & (double HTML encode on server you got the regex from). They're not wrong in Regex syntax, but they do make the expression allow for ;, which isn't allowed in a URL.

Update/explanation

The actual meaning of the error is that because javascript ignores all the backslashes in the string when creating the final regex, it turns into:

/(http|ftp|https)://[w-_]+(.[w-_]+)+([w-.,@?^=%&:/~+#]*[w-@?^=%&/~+#])?/



The original [\w\-_] means "match any word (alphanumeric) character, a hyphen or an underscore".
[w-_] means "match any character between "w" and "_".


But "w" actually comes after "_" (look at an ASCII/UTF-8 character table) meaning it's an invalid range in the character class ("character class" = "collection of characters to match")


January 31, 2012 8:12 PM

Your Answer Here

If you have an other answer, please write its here. Thank you so much!

 

Tips For All - Cisco Exploration CCNA 4.0

Tips For All  - Cisco Exploration CCNA 4.0 Cisco Certified Network Associate Exam,640-802 CCNA, All Answers ccna final ~100/100. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Followers

Share It

Return to top of page Copyright © 2011 | Tips For All Design by: Hainh