Skip to content
This repository has been archived by the owner on Apr 19, 2022. It is now read-only.

Using Regex for email validation #256

Open
antonioOrtiz opened this issue Apr 7, 2020 · 3 comments
Open

Using Regex for email validation #256

antonioOrtiz opened this issue Apr 7, 2020 · 3 comments

Comments

@antonioOrtiz
Copy link

antonioOrtiz commented Apr 7, 2020

Thank you for such a awesome project! It's very elegant!

Package version

"indicative": "^7.4.4",

Node.js and npm version

v13.12.0

At first I used the email rule but noticed a bug where the validation would stop (causing my message to disappear) when reaching the @ symbol, So the user could conceivably enter something like foo@gmail, so I figured why not use a regex; With the Regex the message persists despite entering a valid email...

See below.

Sample Code (to reproduce the issue)

      var data = {
        username: username,
        password: password
      };

      var schema = {
        username: [
          validations.regex([
            new RegExp(
              '/^(([^<>()[]\\.,;: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,}))$/'
            )
          ])
        ],
        password: 'min:7|max:11'
      };
      var messages = {
        regex: 'Make sure this is a valid email!!',
        min: 'The value is too short',
        max: 'The value is too long'
      };

      validateAll(data, schema, messages)
        .then(success => {
          if (success.username) {
            console.log('success.username ', success.username);
            setUsernameError(false);
          }

          if (success.password) {
            console.log('success.password ', success.password);
            setPasswordError(false);
          }
          if (success.username && success.password) {
            console.log('success.username ', success.username);
            setDisableButton(false);
          }
        })
        .catch(errors => {
          console.log('errors ', errors);
          if (errors[0].field === 'username') {
            setUsernameError(true);
            setDisableButton(true);
            setUsernameFeedback(errors[0].message);
          }

          if (errors[0].field === 'password') {
            setPasswordError(true);
            setDisableButton(true);
            setPasswordFeedback(errors[0].message);
          }
        });

BONUS (a sample repo to reproduce the issue)

FormComponent
validate

@samsonoyede
Copy link

This is still an issue

@RomainLanz
Copy link
Member

Hey @FunbiOyede! 👋

It is tough to validate an email, you may not know it, but test@ch is a valid email address.
The only rule to have a valid address is to have @ inside it.

@samsonoyede
Copy link

Yes, @RomainLanz I totally understand what you mean. But I wanted more rules to be involved in the email validation.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants