-2

I want to write an if statement that will check if two fields are empty and to check that one of them is an email address, how do I check email is a valid email address?

 var fullname = $("#name").val();
 var emailAddress = $("#email").val();

      if (fullname.length > 0 && emailAddress.val().length > 0 && emailAddress IS EMAIL) 
    {

         //Do somehting
    }
Ayo Adesina
  • 2,231
  • 3
  • 37
  • 71
  • This will help you https://stackoverflow.com/questions/7635533/validate-email-address-textbox-using-javascript/7635734 – uiTeam324 Apr 14 '20 at 10:20

2 Answers2

0

You can set the type of the input field to email so they must use the format: [smth]@[smth].[smth].

If you want to use javascript you can use regex like: How to validate an email address in JavaScript

0

I'd do it like that:

if (($("#name").val() != '') && ($("#email").val() != '') && $("#email:contains(@)"))
    {
         //Do somehting
    }

It checks if the fields are empty and if the email field includes "@". It isn't exactly foolproof, but it is pretty simple check, so at least you can assume the email is in the right format.

You can also combine wrap it in some function like:

.keyup(function() {

or

.click(function() {

So it will be checked everytime you press a key or click a certain button.

Ezir cz
  • 50
  • 7
  • It's wrong validation checking – uiTeam324 Apr 14 '20 at 10:33
  • @SRana Can you explain why? – Ezir cz Apr 14 '20 at 10:34
  • If any text field have `@ `doesn't mean it's an valid email – uiTeam324 Apr 14 '20 at 10:40
  • @SRana that's exactly what I said in my answer. Read again this part: _so at least you can assume the email is in the right format_. I said **ASSUME**, which means it is possible, that the email is in wrong format, but there is a big chance it is ok... – Ezir cz Apr 14 '20 at 10:55
  • You should not post wrong answers here with some tag line. Post proper solutions and mention some drawback if there is any. Don't post patch work. – uiTeam324 Apr 14 '20 at 10:59