Mike Barwick
Mike Barwick

Reputation: 5367

Prevent Special Characters - jQuery Form Validation

So I have this simple form validation on a text input.

Been doing some Googlin' and can't find how to limit any special characters - meaning I want to prevent the use of anything besides alphanumeric characters.

I want to include it in this function... anyone?

if (!campaign_name || !'^[a-zA-Z 0-9]+$'){
    //alert('Please enter campaign name');
    // Do nothing
    return false;
};

Upvotes: 1

Views: 4645

Answers (1)

gdoron
gdoron

Reputation: 150313

if (/[^a-zA-Z 0-9]+/.test(campaign_name)){
    // error...
}

Upvotes: 2

Related Questions