- This topic is empty.
-
AuthorPosts
-
May 3, 2014 at 3:35 am #169133
dnyanesh
ParticipantHi Traq,
i am trying to validate user input. it working also but it gives all the alert messages at a time.
please correct if anything is wrong in this code. and help me to understand.here is the code:
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>User registration form</title>
<link rel=”stylesheet” type=”text/css” href=”style.css” />
<script src=”//code.jquery.com/jquery-1.9.1.js”></script>
<script scr=”//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js”></script>
<script>
$(document).ready(function()
{
$(“#form1″).validate(
{
rules:
{
username:”required”,
password:
{
password:”required”,
minlength:5
},
firstname:”required”,
lastname:”required”,
email:
{
required:true,
email:true
},
mobile:”required”,
city:”required”,
state:”required”,
country:”required”}, messages: { username: alert("Please enter user name"), password: { required:alert("please provide password"), minlength:"Your password must be contain at least 5 characters long" }, firstname:alert("please enter first name"), lastname:alert("please enter last name"), email:alert("please enter valid email "), mobile:alert("Please enter mobile number"), city:alert("Please enter city"), state:alert("please enter state"), country:alert("Please enter country") }, submitHandler:function(form) { form.submit(); } }); }); </script> </head> <?php $username =''; $password =''; $firstname =''; $lastname =''; $email=''; $mobile=''; $city=''; $state=''; $country=''; if($_POST) { $username =trim($_POST['username']); $password = trim($_POST['password']); $firstname=trim($_POST['firstname']); $lastname=trim($_POST['lastname']); $email=trim($_POST['email']); $mobile=trim($_POST['mobile']); $sex=trim($_POST['sex']); $city=trim($_POST['city']); $state=trim($_POST['state']); $country=trim($_POST['country']); $errors = array(); if (strlen($username)==0) array_push($errors,"please enter User Name"); if (strlen($password)<5) array_push($errors,"Please enter password, password contain more than 5 digit password"); if (strlen($firstname)==0) array_push($errors,"Please enter First Name"); if (strlen($lastname)==0) array_push($errors,"please enter Last Name"); if (!filter_var($email,FILTER_VALIDATE_EMAIL)) array_push($errors,"Please specify valid E-Mail ID"); if (strlen($mobile)==0) array_push($errors,"Please enter Mobile number"); if (strlen($city)==0) array_push($errors,"Please enter city name"); if(strlen($state)==0) array_push($errors,"Please enter state name"); if (strlen($country)==0) array_push($errors,"Please enter country "); if(count($errors)==0) { array_push($errors,"No errors were found. Thanks for valid information"); } $output = ''; foreach($errors as $val) { $output = $output ."<p>$val</p>"; } } error_reporting(0); $con = mysql_connect("localhost","root",""); mysql_select_db("training",$con); if(isset($_POST['submit'])) { $username =$_POST['username']; $password =$_POST['password']; $firstname=$_POST['firstname']; $lastname =$_POST['lastname']; $email =$_POST['email']; $mobile =$_POST['mobile']; $sex =$_POST['sex']; $city =$_POST['city']; $state =$_POST['state']; $country =$_POST['country']; } $sql ="insert into user_frm values('','$username','$password','$firstname','$lastname','$email','$mobile','$sex','$city','$state','$country')"; $rec = mysql_query($sql); if($rec) { echo "data save successfully"; } else { echo "error occurred".mysql_error(); } ?> <body> <?php echo $output; ?> <div> <form id="form1" name="form1" method="post" onsubmit="return validateform()" action=""> <table class="usertbl" align="center" width="600" > <tr style="border-radius:40px"> <td colspan="2" bgcolor="#006600" width="100" align="center" style="color:#CCC" style="font-weight:bold" > User Registration Form </td> </tr> <tr> <td class="tbldata">Username :</td> <td class="tbldata"><input onfocus="if(this.value=='Username')this.value='';" value="Username" class="text" type="text" name="username" /></td> </tr> <tr> <td class="tbldata">Password :</td> <td class="tbldata"><input class="text" type="password" name="password" /></td> </tr> <tr> <td class="tbldata">First Name:</td> <td class="tbldata"><input onfocus="if(this.value=='First Name')this.value='';" value="First Name" class="text" type="text" name="firstname" /></td> </tr> <tr> <td class="tbldata">Last Name:</td> <td class="tbldata"><input onfocus="if(this.value=='Last Name')this.value='';" value="Last Name" class="text" type="text" name="lastname" /></td> </tr> <tr> <td class="tbldata">E-mail :</td> <td class="tbldata"><input onfocus="if(this.value=='E-mail')this.value=''; " value="E-mail" class="text" type="text" name="email" /></td> </tr> <tr> <td class="tbldata">Mobile :</td> <td class="tbldata"><input onfocus="if(this.value=='Mobile')this.value=''; " value="Mobile" class="text" type="text" name="mobile" /></td> </tr> <tr> <td class="tbldata">Sex :</td> <td align="center" ><input type="radio" name="sex" value="Male" checked="checked"> Male </input> <input type="radio" name="sex" value="Female"> Female </input></td> </tr> <tr> <td class="tbldata">City :</td> <td class="tbldata"><input onfocus="if(this.value=='City')this.value='';" value="City" class="text" type="text" name="city" /></td> </tr> <tr> <td class="tbldata">State :</td> <td class="tbldata"><input onfocus="if(this.value=='State')this.value=''; " value="State" class="text" type="text" name="state" /></td> </tr> <tr> <td class="tbldata">Country :</td> <td class="tbldata"><input onfocus="if(this.value=='Enter your Country')this.value=''; " value="Enter your Country" class="text" type="text" name="country" /></td> </tr> <tr> <td class="tbldata"></td> <td class="tbldata" colspan="2" ><input class="text" style="background-color: #6F6" style="border:#F00" type="submit" name="submit" value="Submit" /></td> </tr> </table> </form> </div> </body> </html>
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.