Skip to content Skip to sidebar Skip to footer

Regex Patterns For Html5 Text Field

I would like to create a regex pattern in javascript for a html text field that takes an address. So I only want the user to enter [a-z,A-Z,0-9] and only these symbols [,#-.] I als

Solution 1:

HTML5 gives you the pattern attribute, which allows you to require a certain pattern. In this case, you'd want pattern="[a-zA-Z0-9,#.-]+"

It doesn't matter that older browser don't support this, because you should always validate on the server side.

Solution 2:

$(document).ready(function () {

$('input').keyup(function() {
    var $th = $(this);
    $th.val( $th.val().replace(/[^a-zA-Z0-9,#.-]/g, function(str) { return''; } ) );
});

});

This will work.

Post a Comment for "Regex Patterns For Html5 Text Field"