javascript - Regex working when embedded in CSHTML page but not when moved to an external JS file -


disclaimer. i've eliminated possibility of wrong linkage, incorrect regex etc. already. particular issue isn't duplicate questions. please, kindly take minute read whole before jumping premature dupe flags. it's else basic issues. please give me credit hours spent googling it. thanks.

i noticed inherited lot of pages embedded explicit javascript, decided clean , move script external files. when i'm done, of things seems working except single thing , that's regular expressions. in fact, weird part of them still work expected while others fail.

so when have following set directly in page:

<script src="~/scripts/account.js" type="text/javascript"> $(function(){   var emailregex = /^[a-za-z0-9._-]+@@[a-za-z0-9-]+\.[a-za-z]{2,6}$/;   function isemailvalid(){     return emailregex.test($("#email").val());   } }); </script>  @using (html.beginform("register", "account", formmethod.post)) { <div> ... </div> } 

everything works fine when switch external file, fails ang produces no matches!

i've made sure file correclty linked , methods executed. know sure because rest of effects (like onmouseover etc.) work still. , validation works too. @ first thought there wrong using regular expressions in external files (even if sounds insane) noticed regex works of cases (which more insane).

after few hours, i'm give , move stuff cshtml files, if it's plain wrong , ugly...

you have used escape syntax @-character on razor page (where it's necessary due special meaning of it). when page renders, sequence @@ gets translated @ , resulting actual regex string emitted client follows patter email address.

when moved out js (which indeed great idea, kudos that!), rendition doesn't take place anymore file's jacked in as-is prior (or post, depending on 1 puts it) creating of page.

in pure js file, there's no need it. @ current state, you'd match strings one: blopp@@dopp.pop.

$(function(){   var emailregex = /^[a-za-z0-9._-]+@[a-za-z0-9-]+\.[a-za-z]{2,6}$/;   function isemailvalid(){     return emailregex.test($("#email").val());   } }); 

for more info on , other tricks see e.g. ode code.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -