Malik Shahzad
Malik Shahzad

Reputation: 6959

JQuery validation allow Spanish characters but no special characters

How to validate special characters (like @ # $ %), but allow international (European) language characters?

I'm using jQuery validation.

My current code is:

$('#my_form').validate({
  rules: {
    'model[field]': {
      required: true,
      noSpecialCaracters: true
    }
  }
});

This show errors message on typing special characters like: ö ä å ø æ

Upvotes: 3

Views: 1033

Answers (2)

LoneRanger
LoneRanger

Reputation: 117

$('#my_form').validate({
    rules: {
        'model[field]': {
        required: true,
        noSpecialCaracters:function() { return /^[0-9a-zñáéíóúü]+$/i.test(string); }
    }
}); 

Upvotes: 0

Muggles
Muggles

Reputation: 1556

You can add a custom validation method which uses Regex to validate the input allowing alphabetical letters and the special characters you defined.

$.validator.addMethod('customValidation',
  function(value, element) {
  return this.optional(element) || 
  /^[A-Za-z\u00C0-\u017F]+$/.test(value); },
  'Bad validation message here.');  

Upvotes: 3

Related Questions