Si8
Si8

Reputation: 9225

Modifying a JQuery expression

<script type="text/javascript">
    $(function() {
        $('#theEmail').keyup(function() {
            if (this.value.match(/[^a-zA-Z0-9 ]/g)) {
                this.value = this.value.replace(/[^a-zA-Z0-9 ]/g, '');
            }
        });
    });
</script>

What do I modify to ensure the expression allows me to enter "_" and "-" in the above script?

Upvotes: 0

Views: 106

Answers (2)

Vitthal
Vitthal

Reputation: 546

var pattern = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;

OR

function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\
".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA
-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
} 

Upvotes: 1

Blazemonger
Blazemonger

Reputation: 92893

Change both instances of this regular expression

/[^a-zA-Z0-9 ]/

to

/[^\-_a-zA-Z0-9 ]/

Although as far as I can tell, the initial .match test is completely unnecessary.

Upvotes: 4

Related Questions