0

I have made code with is work great for me, but I want to make email validation to require @ on the field. Here is the code

if (!$('#contact_email').val()) {

        if ($("#contact_email").parent().next(".validation").length == 0) // only add if not added
        {
            $("#contact_email").parent().after("<div class='validation' style='color:red;margin-bottom: 20px;'>Ange e-postadress</div>");
        }
        e.preventDefault(); // prevent form from POST to server
        $('#contact_email').focus();
        focusSet = true;
    } else {
        $("#contact_email").parent().next(".validation").remove(); // remove it
    }

And the input is

<input type="text" class="form-control" placeholder="E-post" name="Email" id="contact_email" onblur="validate()">

I dont use basic email field because I don't want to be on english. How can i implement @ to be required on this text input. Thank you

DroDro
  • 7
  • 6

3 Answers3

0

You can use regular expression to check for an "@" and a "." field.

Regex:

var regex= /\S+@\S+\.\S+/;
return regex.test(email);

The above code will return true if the regular expression is matched. This expression checks for an @ and a . to be present in the given string. Heres the documentation on .test http://www.w3schools.com/jsref/jsref_regexp_test.asp

I've noticed other answers have better regular expressions as the above will allow for multiple @'s and .'s to be present.

Vistari
  • 697
  • 9
  • 21
0

Hey Use This function:

function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);

}

found here:

Validate email address in JavaScript?

Community
  • 1
  • 1
osanger
  • 2,276
  • 3
  • 28
  • 35
0

Try this for email validation:

function isEmail(email) {
        var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
        if (email.match(mailformat)) {
            return true;
        } else {
            return false;
        }
    }

Checking as follows:

if (email != "") {
                if (!isEmail(email)) {
                    alert("invalid");
                }
            }
Santhucool
  • 1,656
  • 2
  • 36
  • 92