我有一个表单html,其中包含出生日期和电子邮件字段。我正在使用日期选择器为出生地的bate工作正常。对于电子邮件领域。我使用ajax验证email.email验证不起作用。 我使用以下代码,
HTML
<?php
session_start();
ob_start();
?>
<!DOCTYPE html>
<html>
<head><title>TIME MANAGEMENT</title>
<style>
body { margin: 0; padding: 0; font-family:Verdana; font-size:20px }
td { padding: 2px; font-family:Verdana; font-size:10px }
.input_s1_normal { width: 150; height: 15; font-family: Verdana; font-size: 10px; border: 1px solid black; }
.input_s1_focus { background: #efefef; }
.button_s1 { font-family: Verdana; font-size: 10px; font-weight: bold; border: 1px solid black; }
</style>
<link rel="stylesheet" href="css/jquery-ui.css">
<link rel="stylesheet" href="css/registration.css">
<script src="js/jquery-1.2.6.min.js" type="text/javascript"></script>
<script src="js/jquery-1.10.2.js"></script>
<script src="js/jquery-ui.js"></script>
<script>
$(function() {
$( "#datepicker" ).datepicker({ dateFormat: "yy-mm-dd" }).val()
});
</script>
<script src="js/check.js" type="text/javascript"></script>
</head>
<body>
<form name="chkForm" id="chkForm" method="POST" action="condition.php">
Name<input type="text" name="empname" value=""/><br/>
<p>
DOB <input type="text" id="datepicker" name="empdob" value="" /><br/><!--d-m-y-->
</p>
<?php
$link = mysql_connect("localhost", "root", "");
mysql_select_db("harisan", $link);
$result = mysql_query("SELECT * FROM designation", $link);
$num_rows = mysql_num_rows($result);
if($num_rows>0)
{
echo "Designation <select name='position'><br/>";
while($result1= mysql_fetch_array($result))
{
echo "<option value='".$result1['design_name']."'>".$result1['design_name']."
</option>";
}
echo "</select>";
}
?>
<br/>
Email<input id="email" type="text" name="email" /><br/>
<div id="emailInfo" align="left"></div>
<input type="submit" name="signup" value="submit"></input>
<a href="view_list.php" value="">view</a>
</form>
</body>
</html>
javascript
$(document).ready(function(){
$('#B1').attr('disabled','');
var emailok = false;
var boxes = $(".input_s1_normal");
var myForm = $("#chkForm"), email = $("#email"), emailInfo = $("#emailInfo");
//give some effect on focus
boxes.focus(function(){
$(this).addClass("input_s1_focus");
});
//reset on blur
boxes.blur(function(){
$(this).removeClass("input_s1_focus");
});
//Form Validation
myForm.submit(function(){
if(email.attr("value") == "")
{
alert("Enter Email");
email.focus();
return false;
}
if(!emailok)
{
alert("Check Email");
email.attr("value","");
email.focus();
return false;
}
});
//send ajax request to check email
email.blur(function(){
$.ajax({
type: "POST",
data: "email="+$(this).attr("value"),
url: "check.php",
beforeSend: function(){
emailInfo.html("<font color='blue'>Checking Email...</font>");
},
success: function(data){
if(data == "invalid")
{
emailok = false;
emailInfo.html("<font color='red'>Invalid Email</font>");
}
else if(data != "0")
{
emailok = false;
emailInfo.html("<font color='red'>Email Already Exist</font>");
}
else
{
emailok = true;
emailInfo.html("<font color='green'>Email OK</font>");
}
}
});
});
});
我不知道是什么问题。请帮忙 我是javascript的新手。电子邮件验证无法正常工作。它总是在重新显示无效的电子邮件。 我使用了以下网址:https://beski.wordpress.com/2009/05/16/check-email-already-exist-ajax-jquery/
答案 0 :(得分:1)
听起来你的问题在你的check.php里面(你没有包括)。如果你在check.php中使用正则表达式来验证你的电子邮件我试着用这个代码替换它,看看它是否有效:
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
//Valid email!
}