Parsley + Unable to verify email address For full width character

Trying to write on email validation check for full character loop: -

JS code: -

window.Parsley.addValidator('validateFullWidthCharacters', {
  validateString: function(_value) {
    regex = /[\u3000-\u303F]|[\u3040-\u309F]|[\u30A0-\u30FF]|[\uFF00-\uFFEF]|[\u4E00-\u9FAF]|[\u2605-\u2606]|[\u2190-\u2195]|\u203B/;
    if (regex.test(_value)) {
      return false;
    } else {
      return true;
    }
  },
  messages: {
    en: 'Please enter a valid email address.'
  }
});

      

Works fine and checks email by sending error message

rahul@mail.com

but if i change the letter below one doesn't work

rahul@mail.com

<input placeholder="e.g. mail@example.com" class="form-control parsley-success" id="user_email" data-parsley-required="" data-parsley-type="email" data-parsley-validate-full-width-characters="true" type="email" value="" name="user[email]" data-parsley-id="17"> 

      

Please give a suggestion to allow this.

+3


source to share


2 answers


Your regex just matches full width characters, you need to check your value with normal characters.

There is a bug, or perhaps "expected behavior" with input type = "email" that it converts full-width characters to regular characters if valid before @.

The solution for you is to change type = "email" to type = "text" and use a different template for email and change it if you return regex.test instead

regex = /^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/gm

      

New test

regex = /^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/gm
/^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/gm
regex.test('rahul@ο½ο½ο½‰ο½Œ.com')
false
regex.test('ο½’ο½ο½ˆο½•ο½Œ@mail.com')
false
regex.test('rahul@mail.com')
true

      



your code will become:

window.Parsley.addValidator('validateFullWidthCharacters', {
  validateString: function(_value) {
    regex = /^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/gm;
    return regex.test(_value);
  },
  messages: {
    en: 'Please enter a valid email address.'
  }
});

      

Html

<input placeholder="e.g. mail@example.com" class="form-control parsley-success" id="user_email" data-parsley-required="" data-parsley-type="email" data-parsley-validate-full-width-characters="true" type="text" value="" name="user[email]" data-parsley-id="17"> 

      

    window.Parsley
        .addValidator('validateFullWidthCharacters', {
            requirementType: 'string',
            validateString: function (value, requirement) {
                regex = /^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/gm;
                return regex.test(value);
            },
            messages: {
                en: 'Please enter a valid email address.'
            }
        });
      

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/parsley.js/2.7.2/parsley.min.js"></script>
</head>
<body>

<form data-parsley-validate>
    <input type="text" name="email" placeholder="e.g. address@example.ext" data-parsley-validate-full-width-characters="true">
    <button>Submit</button>
</form>

</body>
</html>
      

Run code


+3


source


Try to remove any extra spaces from the string before running validation on the email string.

Like this



tr = str.replace(/\s+/g, '');

+1


source







All Articles