Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I difference id (number) and email (string) in Javascript?

I have API and some of them return to only email and some return only id. For example:

user1
value: example@example.com

user2
value: 1212391361783212

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I need that if I have input and wanna give value if the value is email. If the value is id, the input value must be null

<input value={???}/>

>Solution :

Your code sample is incomplete but what you’re trying to do should look something like this. Rather than assuming any non-numerical response is a valid email, which can break in the future as the API changes, be explicit about what you’re looking for as below.

// Assume you're comfortable using a relatively loose pattern to match an email i.e. no overly strict formatting rules
const mailPattern = /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/
// Assume response is in scope and contains the API response with the email or id deserialized from the body
const value = mailPattern.test(response) ? value : null
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading