question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to validate a field as an email if it contains the "@" character

See original GitHub issue

Hey,

Is it possible to conditionally validate a field based on its own content? I know it’s possible to do conditional validation based on another input using .when() , but couldn’t find anything using the field content itself.

Thank you in advance for your response

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
elrhaffcommented, Mar 30, 2021

Yep, this worked

export const LoginSchema = Yup.object().shape({
  usernameOrEmail: Yup.string().when(".", {
    is: (val: string) => (val && val.includes("@")), //check val is defined and includes @
    then: emailValidator,
    otherwise: usernameValidator,
  }
});

Thanks a lot @jquense !

1reaction
jquensecommented, Mar 30, 2021

Try '.' for the field name

Read more comments on GitHub >

github_iconTop Results From Across the Web

4.1. Validate Email Addresses - Regular Expressions ...
This first solution does a very simple check. It only validates that the string contains an at sign (@) that is preceded and...
Read more >
JavaScript: HTML Form - email validation - w3resource
In this page we have discussed how to validate an email using JavaScript : An email is a string (a subset of ASCII...
Read more >
How can you check if an input (email) field contains a special ...
1. All you need is $("input[name='emailAddress']").val(); before checking with the regex. – Pointy · @Pointy Do you have an example? – Spanky.
Read more >
How to Do an Email Validation in JavaScript? - Simplilearn
One of the most popular ways of validating email in JavaScript is by using regular expressions. JavaScript uses regular expressions to describe ...
Read more >
How to verify that strings are in valid email format
The example defines an IsValidEmail method, which returns true if the string contains a valid email address and false if it doesn't but...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found