-1

I have been studying JS for a while, self-taught and I wonder how to put this on JS

Like Required properties in HTML, if user forgot to input his @ symbol, HTML validition will show message, and etc.

I knew some use pattern="" attribute, W3 pattern

I don't really know how to use regex, and don't even understand their symbols

How to put this in JS if condition for email input ??

Any help will be appreciated Thanks

Gwein
  • 23
  • 7
  • 1
    There are no shortcuts here. You won't regret learning Regex. This tool has helped me a great deal over the past few years: https://regexr.com/2rhq7. The link I've provided is to a community created Regex for Email. Regexr lets you play with the regex. Type an email address into the text area in the middle of the page, and see what matches. It's a great way to learn by doing. – Harvey A. Ramer Nov 17 '18 at 03:58
  • https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript?rq=1 – K Scandrett Nov 17 '18 at 04:04
  • Possible duplicate of [How to validate an email address in JavaScript?](https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript) – Ouroborus Nov 17 '18 at 04:20

1 Answers1

0

If txtEmail is id of text field required to input email and btnSubmit is the id of submit button then try following code (jQuery required):

$("#btnSubmit").on("click",function(e){
    e.preventDefault();
    //Email Verification
    if(!$("#txtEmail").val().match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/)){
         alert("Invalid EMAIL");
         $("#txtEmail").focus();
         return false;
    }
});
Twinkle
  • 191
  • 2
  • 12
  • In place of `$("#btnSubmit").on("click",function(e){` you can also verify email at `onblur` event. Put `$("#txtEmail").on("blur",function(e){`. `e.preventDefault();` will not be required in that case. – Twinkle Nov 17 '18 at 03:57