-1

I am working on a mvc 5 project .in contact us page I want user to send admin his / her emaiul address .so I want to validate email in javascript on that page .I wrote some code that does not work properly. I want you to help me plesae.

<script language="javascript">
    function f1() {
        var inputText = document.getElementById("email").value;
        var mailformat = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (inputText.value.match(mailformat)) {
            document.form1.text1.focus();

        }
        else {
            alert("You have entered an invalid email address!");
            document.form1.text1.focus();
            event.preventDefault();
        }
    }
</script>
basloogh
  • 1
  • 4
  • And what does "code that does not work properly" actually mean? – arkascha Mar 28 '16 at 07:26
  • BTW: TLDs can have more than 4 characters. An example might be `.museum` or `.rocks`. – arkascha Mar 28 '16 at 07:27
  • What do you mean by not working properly? – Tim Mar 28 '16 at 07:27
  • 1
    visit this http://stackoverflow.com/questions/46155/validate-email-address-in-javascript –  Mar 28 '16 at 07:29
  • If your using asp.net-mvc, then why are you not using the `[EmailAddress]` attribute on you property and `@Html.ValidationMessageFor()` so you get server and client side validation –  Mar 28 '16 at 07:32
  • Completely it does not work.I want to use client - side validation – basloogh Mar 28 '16 at 07:35
  • you dont't want value in `if (inputText.value.match(mailformat))` because inputText is already a value so `if (inputText.match(mailformat))` is correct... down below see my answer – sharma sk Mar 28 '16 at 07:41
  • @basloogh, What does not work? Using `[EmailAddress]` and `@Html.ValidationMessageFor()` gives you both client side and more importantly server side validation, which you now need to also implement manually. –  Mar 28 '16 at 07:45

4 Answers4

0
function validateEmail(email) {
    var re = ([\w-+]+(?:\.[\w-+]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7});
    return re.test(email);
}
Sanya Zahid
  • 466
  • 6
  • 18
  • Hi, this answer is a coppy from here - http://stackoverflow.com/questions/46155/validate-email-address-in-javascript/46181#46181 – Abrar Jahin Mar 28 '16 at 07:37
0

A basic HTML and JS working code for validating email with JS is given here for u-

function validateForm()
{
    var x = document.forms["myForm"]["email"].value;
    var atpos = x.indexOf("@");
    var dotpos = x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
    {
        alert("Not a valid e-mail address");
        return false;
    }
    else
    {
        alert("Valid e-mail address");
        return true;
    }
}
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm();" method="post">
    Email: <input type="text" name="email">
    <input type="submit" value="Submit">
</form>

Think, u have your answer :)

Abrar Jahin
  • 13,970
  • 24
  • 112
  • 161
0
<form name="form" action="#" onSubmit="return f1()" method="POST">
    <input type="email">
</form>
<script>
function f1(){
    var email = document.forms["form"]["Email"].value;
    var regex = /^([0-9a-zA-Z]([-_\\.]*[0-9a-zA-Z]+)*)@([0-9a-zA-Z]([-_\\.]*[0-9a-zA-Z]+)*)[\\.]([a-zA-Z]{2,9})$/;
    if(!regex.test(email)){
        alert("You have entered an invalid email address!");
        return false;
    }
}  
</script>       
Ahmed
  • 1,666
  • 17
  • 23
0

You don't want inputText.value only inputText like this

<input type="text" id="email" />
<input type="submit" onClick="f1()"/>


<script language="javascript">
    function f1() {
        console.log(document.getElementById("email").value);
        var inputText = document.getElementById("email").value;
      console.log(inputText)
        var mailformat = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (inputText.match(mailformat)) { // <<<<<<< here
            //document.form1.text1.focus();
            alert("correct")

        }
        else {
            alert("You have entered an invalid email address!");
            document.form1.text1.focus();
            event.preventDefault();
        }
    }
</script>
sharma sk
  • 651
  • 4
  • 14