Tuesday, August 28, 2012

Simple Javascript Regex

some overview about what JS offer in terms of regex
http://www.javascriptkit.com/javatutors/redev3.shtml
more info about the use of RegExp object
\http://www.w3schools.com/jsref/jsref_obj_regexp.asp
and a simple example i found somewhere for PW check:

$(function () {
   $('#txtNewPW').keyup(function () {
      $('div.errorRegexPw').hide();
      var inputVal = $(this).val();
      var numericReg = /<%= PWRegex %>/;
      if (!numericReg.test(inputVal)) {
          $(this).after('<div class="errorRegexPw">Not A Valid Password.</div>');
      }
   });
});

thing is that to init the RegExp obj u just need to do var regex = /pattern/; with the 2 '/'.
now all u need to do is regex.test(string_to_test) and get true or false like:

var regex = /ariel/;
var t = regex.test("ariel");
var f = regex.test("arik");
here t will be true and f will be false

No comments:

Post a Comment