JQuery - remove the chars not matching regEx

Posted by JQueryBeginner on Stack Overflow See other posts from Stack Overflow or by JQueryBeginner
Published on 2011-01-09T21:49:26Z Indexed on 2011/01/09 21:53 UTC
Read the original article Hit count: 201

Filed under:
|

Hi All,

I am trying to use jquery for validating forms.

This is the pattern that is allowed in a text box for a user. var pattern = /^[a-zA-Z0-9!#$&%*+,-./: ;=?@_]/g; If the user types anything else other than this then that has to be replaced with a "".

$(document).ready(function() {
  $('#iBox').blur(function() {
     var jVal = $('#iBox').val();
  if(jVal.match(pattern)) {
   alert("Valid");
  } else {
   alert("New "+jVal.replace(!(pattern),""));
                }
    });
  });
});

But the replace function does not work this way.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jquery-validate