Tuesday, 10 March 2015

javascript validation

<!DOCTYPE html>
<html>

<head>
<title>Untitled Document</title>
<meta charset="UTF-8">
<script type="text/javascript">

function validateform()
{
//fname
   if(document.f1.txtfname.value == "")
   {
     document.getElementById("msg1").innerHTML="Enter First Name";
     document.f1.txtfname.focus();

        return false;
   }
   //txtlname
   if(document.f1.txtlname.value == "")
   {
   document.getElementById("msg2").innerHTML="Enter Last Name";
      document.f1.txtlname.focus();
   return false;
   }
   //email
    if( document.f1.txtename.value == "" )
   {
     alert( "Please provide your Email!" );
     document.f1.txtename.focus() ;
     return false;
    }
    //confirmpass
    var txtpass1 =document.getElementById("txtpass1").value;
    var txtpass2 = document.getElementById("txtpass2").value;
    if(txtpass1 != txtpass2)
    {

            document.getElementById("txtpass23").innerHTML="wrong";
            document.f1.txtpass2.focus();
            return false;
    }
    else
    {
   
   
    }
    //date
    if( document.f1.dt1.value == "" )
   {
     document.getElementById("msg4").innerHTML="select Date";
     document.f1.dt1.focus() ;
     return false;
    }
//Radio Button   
     var radios = document.getElementsByName("g1");
    var formValid = false;

    var i = 0;
    while (!formValid && i < radios.length) {
        if (radios[i].checked) formValid = true;
        i++;       
    }

    if (!formValid)
    {
     document.getElementById("msg5").innerHTML="select Gender";
    return formValid;
    }
    //chackbox
     var radios1 = document.getElementsByName("c1[]");
    var formValid1 = false;

    var i1 = 0;
    while (!formValid1 && i1 < radios1.length) {
        if (radios1[i].checked) formValid1 = true;
        i1++;       
    }

    if (!formValid1)
    {
     document.getElementById("msg6").innerHTML="select Hobby";
    return formValid1;
    }
    //city
var e = document.getElementById("txtcity").value;

if(e==0) //for text use if(strUser1=="Select")
{
alert("select City");
}
   
    //end js
   
   
       

  
        return( true );
}


</script>


</head>

<body>
<form action="#" method="post" name="f1" onsubmit="return(validateform());">

<table border="1">
    <tr>
        <th>First Name</th>
        <td><input type="text" name="txtfname" /></td>
        <td><span id="msg1"></span></td>
    </tr>
   
    <tr>
        <th>Last Name</th>
        <td><input type="text" name="txtlname" /></td>
        <td><span id="msg2"></span></td>   
    </tr>
    <tr>
        <th>Email</th>
        <td><input type="text" name="txtename" /></td>
    </tr>
    <tr>
        <th>Password</th>
        <td><input id="txtpass1" type="password" name="txtpass" /></td>
       
   
    </tr>
    <tr>
        <th>Confirm Password</th>
        <td><input id="txtpass2" type="password" name="txtconpass" /></td>
        <td><span id="txtpass23"></span></td>
   
    </tr>
    <tr>
        <th>Date OF Birth</th>
        <td><input type="date" name="dt1"  /></td>
        <td><span id="msg4"></span></td>
   
    </tr>   
    <tr>
        <th>Gender</th>
        <td>
            <input type="radio" name="g1" value="male" />Male
            <input type="radio" name="g1" value="female" />Female
        </td>
        <td><span id="msg5"></span></td>
   
    </tr>
    <tr>
        <th>Hobby</th>
        <td><input type="checkbox" name="c1[]" value="cri" />Cricket
            <input type="checkbox" name="c1[]" value="read" />Read
        </td>
        <td><span id="msg6"></span></td>
    </tr>
    <tr>
        <th>City</th>
        <td><select name="txtcity" id="txtcity" onChange="validateform()">
                <option value="0">select</option>
                <option value="1">Kalol</option>
                <option value="2">Amedabad</option>
                <option value="3">Patan</option>
                <option value="4">Maheshana</option>           
                <option value="5">Baroda</option>
            </select>
        </td>
        <td><span id="msg7"></span></td>
    <tr>
    <tr>
        <td colspan="3" align="center"><input type="submit" name="add" value="ADD" onblur="validateform()" onclick="validateform()" /></td>
   
    </tr>   
   
</table>


</form>
<?php /*?>
    <?php
        include('connection.php');
       
        if(isset($_POST['add']))
        {       
        $fname=$_POST['txtfname'];
        $lname=$_POST['txtlname'];
        $txtename=$_POST['txtename'];
       
        $txtconpass=$_POST['txtconpass'];
        $dt=$_POST['dt'];
        $mo=$_POST['mo'];
        $yr=$_POST['yr'];
        $dtat1=$dt."/".$mo."/".$yr;
        echo $dtat1;
        $gender=$_POST['g1'];
        $hobby=implode(',',$_POST['c1']);
        $txtcity=$_POST['txtcity'];

        $sql=mysql_query("insert into task1 values('','$fname','$lname','$txtename','$txtconpass','$dtat1','$gender','$hobby','$txtcity')");               
        if(!$sql)
        {
        echo mysql_error();
        }
        else
        {
            echo "insert";
        }
}
    ?><?php */?>
</body>

</html>

No comments:

Post a Comment