Skip to main content

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"


Comments

  1. 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")

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.