本文介绍了使用jQuery进行表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的表单验证一次都不起作用
my form validations are not working check once
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="www.w3/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Valliation</title> <script type="text/javascript" src="jquery-1.8.0.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("form.register").change(function() { $.post("check.php", $("form.register").serialize(), function( data ) { if( data.username == "inuse" ) $("p#username_error").slideDown(); else $("p#username_error").hide(); if( data.contact == "nocontact" ) $("p#contact_error").slideDown(); else $("p#contact_error").hide(); if( data.email == "notvalid" ) $("p#email_error").slideDown(); else $("p#email_error").hide(); if( data.location == "nolocation" ) $("p#location_error").slideDown(); else $("p#location_error").hide(); if( data.place == "noplace" ) $("p#place_error").slideDown(); else $("p#place_error").hide(); if( data.bhk == "nobhk" ) $("p#bhk_error").slideDown(); else $("p#bhk_error").hide(); if( data.brs == "nobrs" ) $("p#brs_error").slideDown(); else $("p#brs_error").hide(); if( data.area == "noarea" ) $("p#area_error").slideDown(); else $("p#area_error").hide(); if( data.amount == "noamount" ) $("p#amount_error").slideDown(); else $("p#amount_error").hide(); }, "json"); }); }); </script> </head> <body> <form action="register.php" method="post" class="register"> <p align="center">POST YOUR PROPERTY HERE</p> <table width="315" border="0" cellspacing="1" cellpadding="7"> <tr> <th scope="row">For:</th> <td><select name="type"> <option value="sale">Sale</option> <option value="rent">Rent</option> <option value="share">Share</option> </td> </tr> <tr> <th scope="row">Name:</th> <td> <input type="text" name="username" id="txtname" /> <p id="username_error" class="error">That Username is unavailable</p> </td> </tr> <tr> <th scope="row">Contact:</th> <td> <input type="text" name="contact" /> <p id="contact_error" class="error">Contact Number is unavailable</p> </td> </tr> <tr> <th scope="row">Email:</th> <td><input type="text" name="email" /> <p id="email_error" class="error">Email is not valid</p><br/> </td> </tr> <tr> <th scope="row">Property: </th> <td> <select name="property"><option value="Buildings">Buildings</option> <option value="site">Site</option> <option value="land">Land</option></td> </tr> <tr> <th scope="row">Property Type:</th> <td> <select name="ptype"><option value="Residencial">Residencial</option> <option value="commercial">Commercial</option> <option value="apartments">Apartments</option> </td> </tr> <tr> <th scope="row">Location: </th> <td> <input type="text" name="location" /> <p id="location_error" class="error">Location is unavailable</p> </td> </tr> <tr> <th scope="row">Place: </th> <td> <input type="text" name="place" /> <p id="place_error" class="error">Place is unavailable</p> </td> </tr> <tr> <th scope="row">BHK: </th> <td> <input type="text" name="bhk" /> <p id="bhk_error" class="error">BHK is unavailable</p> </td> </tr> <tr> <th scope="row">Bathrooms: </th> <td> <input type="text" name="brs" /> <p id="brs_error" class="error">Enter Bathrooms number</p> </td> </tr> <tr> <th scope="row">Area: </th> <td> <input type="text" name="area" /> <p id="area_error" class="error">Area is unavailable</p> </td> </tr> <tr> <th scope="row">Amount: </th> <td> <input type="text" name="amount" /> <p id="amount_error" class="error">Amount is unavailable</p> </td> </tr> <tr> <th scope="row"> <input type="submit" name="submit" value="register" onsubmit="return validateUsername()";> </th> <td> <input type="file" name="file" /></tr> <tr> <th scope="row"> </th> </tr> </form> </table> </body> </html>而我的check.php是
and my check.php is
<?php $data = array( "username" => "", "contact" => "", "email" => "", "location" => "", "place" => "", "bhk" => "", "brs" => "", "area" => "", "amount" => "" ); if( isset($_POST["username"]) ) { if( in_array( $_POST["username"], $usernames) ) { $data["username"] = "inuse"; } } if( isset($_POST["contact"]) ) { if( $_POST["contact"] != "" && is_numeric($_POST["contact"] ) { $data["contact"] = "no contact"; } } if( isset( $_POST["email"] ) ) { if( $_POST["email"] != "" && !preg_match( "/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST["email"] ) ) { $data["email"] = "notvalid"; } } if( isset($_POST["location"]) ) { if( $_POST["location"]!= "" ) { $data["location"] = "nolocation"; } } if( isset($_POST["place"]) ) { if( $_POST["place"] != "" ) { $data["place"] = "noplace"; } } if( isset($_POST["bhk"]) ) { if( $_POST["bhk"] != "" && is_numeric($_POST["bhk"] ) { $data["bhk"] = "nobhk"; } } if( isset($_POST["brs"]) ) { if( $_POST["brs"] != "" && is_numeric($_POST["brs"] ) { $data["brs"] = "nobrs"; } } if( isset($_POST["area"]) ) { if( $_POST["area"] != "" && is_numeric($_POST["area"] ) { $data["area"] = "noarea"; } }if( isset($_POST["amount"]) ) { if( $_POST["amount"] != "" && is_numeric($_POST["amount"] ) { $data["amount"] = "noamount"; } } echo json_encode( $data ); mysql_close($con); ?> 推荐答案 (document).ready(function(){ (document).ready(function(){ ("form.register").change(function() { ("form.register").change(function() { .post("check.php", .post("check.php",更多推荐
使用jQuery进行表单验证
发布评论