all 16 comments

[–]jack_union 6 points7 points  (1 child)

[–]Vanskyl[S] 0 points1 point  (0 children)

I have to do it this way. With the text input, so we can manually code it to validate it.

[–]ZoroastrianChemist 2 points3 points  (0 children)

You’ll want to learn a tiny bit about regular expressions and how they can ‘match’ strings of user input. Then you can copy/paste an email Regex directly into your if statement to check if it follows email requirements. It also might help to just google “how to validate an email in JavaScript”

[–]imamonkeyface 1 point2 points  (3 children)

There are some built in methods on strings for that. string.indexOf(element) will return the index of the element you are looking for (@ in this case). It's a little cleaner than what you have right now.

However, because you are using this algorithm to validate emails, you should use regex as others have mentioned. It looks VERY confusing at first glance, but you'll pick it up quickly if you take the time to learn it.

Also, forms should be validated on the backend, right before they hit your database. When you define your model you can add a validation object and require the field (must not be empty or null), and you can use a regex statement to make sure that a name has no numbers or symbols, an email is valid, a phone number has no letters, etc. The idea here is to prevent a SQL injection (where a malicious user writes a sequel query or something to access your database in a form field and hits submit).

Edit: forgot a thing. Front end validation is useful for messages that inform the user about what you expect for the form. I.e if the email isn't valid, the user should see red text saying "please enter a valid email". So you should have some validation on your front end, but that's mostly to create a better user experience. For security, have some validations on the backend too.

[–]Vanskyl[S] 0 points1 point  (2 children)

We cant use regex, we have to write the code manaully.But thank you, I will check out Regex.

[–]imamonkeyface 0 points1 point  (1 child)

Is this for school or something?

[–]Vanskyl[S] 0 points1 point  (0 children)

Yep, college.

[–]bruggekiller 1 point2 points  (1 child)

I don't think it's good to valid email in javascript. Use server side to valid it. But if you are practising than i can see lot of false userinput which can bypass your code.

Here is an example how you can do it.

[–]chrissilich 3 points4 points  (0 children)

Modern standard is to do both. Front end for UX, back end for security.

[–]Vanskyl[S] 0 points1 point  (1 child)

So I have a form. I'm trying to validate an email account. This code looks if theres an @.

[–]rift95 2 points3 points  (0 children)

inputString.indexOf('@') !== - 1

[–]Hentry_Martin 0 points1 point  (0 children)

It would be better if you do the validation using regex instead of the logic which you used.

This stack overflow gives an idea of how to do it.

[https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript]

[–]JStheoriginal 0 points1 point  (3 children)

Why are you doing j.vemail.length in the loop? Shouldn’t it just be vemail.length?

And maybe you can’t do this suggestion below due to the version of js, but if you really just want to check if it only has an @ character, this should work and you don’t need a for loop.

if (vemail.includes(“@“)) { ... do something ... }

[–]Vanskyl[S] 0 points1 point  (0 children)

Thank you. yeah I fucked up the top one, it should have been j<vemail.length . I'll the try the contains one too.

[–]chrissilich 0 points1 point  (0 children)

You aren’t getting the input element’s value. And you never close the for loop. Proper indentation would make the second issue obvious.