jQuery cannot take the user input when the form is submitted
I have the following piece of code. I need to check the validation of user
input (his email address) while he submits the form. And if the email
address doesn't have a correct format, an error message should be shown.
The problem is that when I enter a wrong email address in the textbox and
press the submit button nothing happens. I used to check the code using
alert (I wrote alert("Email")) and understood that
$("#customerEmail").val() returns no value, while I have currently entered
a value in the textbox. What could be the problem?
<form method="post" action="">
<input type="text" id="customerEmail"/>
<br/>
<input type="submit" id="customerRegister" name="customerRegister"/>
</form>
<?php
if(isset($_POST['customerRegister'])){
echo'
<script type="text/javascript"
src="Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
var EMail=$("#customerEmail").val();
alert(Email);
var atpos = EMail.indexOf("@");
var dotpos = EMail.lastIndexOf(".");
if (EMail != "")
{
if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= EMail.length)
{
$("#customerEmailSpan").html("wrong email address");
}
else if (atpos >= 1 && dotpos >= atpos + 2 && dotpos + 2 <
EMail.length)
document.getElementById("customerEmailSpan").innerHTML = "";
}
</script>';
}
?>
No comments:
Post a Comment