1

How to show text in id="result" When not a invalid email ?

http://jsfiddle.net/d65cdh2v/

When i fill not a valid email EG: xxxxxxx in input name="email" i want to show text in id="result" EG: xxxxxxx not a valid

<!DOCTYPE html>
<html>
<head>
<script>
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) {
        $("#result").text("");
        $("#result").text(x + " not a valid :)");
        return false;
    }
    else {
        alert("OK");
    }
}
</script>
</head>

<body>
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>

<h2 id='result'></h2>

</body>

</html>
nomwery geqoilu
  • 109
  • 2
  • 10

1 Answers1

1

Your code works perfectly fine, provided you include jQuery: http://jsfiddle.net/d65cdh2v/1/

But I assume you aren't using jQuery, judging by the rest of your code, so to do it with vanilla JavaScript:

document.getElementById('result').innerHTML = x + " not a valid :)";

JSFiddle

George
  • 36,413
  • 9
  • 66
  • 103
  • When not a invalid email, can i hide or show element ? – nomwery geqoilu Oct 18 '14 at 16:31
  • Of course. You would could change the `.style.display` property to `none`, which sets the CSS style: http://stackoverflow.com/questions/6242976/javascript-hide-show-element – George Oct 18 '14 at 16:32