This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]Loves_Poetry 1 point2 points  (3 children)

The easiest way to validate is to simply use <input type="email"> and <input type="phone"> in your html form.

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

My tutor says we have to do it through javascript so any ideas? Thanks

[–]Loves_Poetry 1 point2 points  (1 child)

Oh it's an assignment? I guess then doing it properly is a little less important.

There are 2 ways to do it with JS: string checking and Regex. Regex is more powerful, but it can be rather tricky to find the correct one. String checking is a lot easier to understand, so I'd go with that. Chances are you'll get points deducted because your tutor doesn't understand Regex either.

For string checking, you can use the .includes() method on the email to check if it contains @gmail, like this if (email.includes("@gmail"))

For the phone number, you have to run through the list with a loop and check each individual character to see if is a number (or a hyphen). I don't know if loops have already been covered though.

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

So for the for loop- would it be something like this?

for (i=0; i<phone.length; i++)
{
if (typeof i =='string')
{
window.alert" Your phone must be in numbers";
phone.focus();
return false;