Friday, February 25, 2011

JQuery Form Validation

How to validate form with JQuery

jquery form validation examples,plugins,download,submit,Validate HTML form use JQuery

For validate form use jquery, you have to download jquery validation plugins or give full path of jquery validation plugins file like folowing example.


Insert this JavaScript in the head section of your html code.

<script src="http://code.jquery.com/jquery-1.5.min.js" type="text/javascript"></script>
<script src="http://view.jquery.com/trunk/plugins/validate/jquery.validate.min.js" type="text/javascript"></script>

<script>
$(document).ready(function(){
$("#valForm").validate({
   rules:{
      firstName:{
           required:true
   },
   email:{
          required:true,
         email:true
   },
   password:{
         required:true,
   },
   confirmPassword:{
        required:true,
        equalTo: "#password",
   },
},
  messages:{
      firstName:{
           required:" First Name cant be empty"
     },
     email:{
          required:" Please enter valid Email Address"
     },
     password:{
         required:" Please enter Password"
     },
    confirmPassword:{
        required:" Please confirm password",
        equalTo:" Confirm password did not match"
    },
 },
});
});
</script>



<table border="0" cellpadding="2" cellspacing="1">
<tr>
<td>First Name</td>
<td><input name="firstName" id="firstName" type="text"></td>
</tr>
<tr>
<td>Email</td>
<td><input name="email" id="email" type="text"></td>
</tr>
<tr>
<td>Password</td>
<td><input name="password" id="password" type="password"></td>
</tr>
<tr>
<td>Confirm Password</td>
<td><input name="confirmPassword" id="confirmPassword" type="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>

Demo





First Name
Email
Password
Confirm Password

Note: If you want customize error message style you can add this style in head section of your HTML file

<style type="text/css">
    label.error {
         font-size:.8em;
         color: red;
         padding-left: .5em;
         vertical-align:top;
   }
</style>

Tuesday, February 8, 2011

JavaScript Domain Name Validation


How to Validate Domain Name


We are use variety of basic rules for domain name.
  • Use only letters, numbers, or hyphen ("-")
  • Cannot begin or end with a hyphen
  • Must have less than 63* characters

JavaScript Code

Insert this JavaScript in the head section of your html code.
<script type="text/javascript">
 function validate_domain(){
   var txt_domain = document.getElementById('txt_domain').value;
   var domain_array = txt_domain.split('.');

  var domain = domain_array[0];
//This is reguler expresion for domain validation
  var reg = /^([A-Za-z0-9])+[A-Za-z0-9-]+([A-Za-z0-9])$/;

if(domain == ''){
     alert("Please enter the domain name"); 
     document.getElementsById('txt_domain').focus();
     return false; 

if(reg.test(domain) == false){
   alert("Invalid character in domain. Only letters, numbers or hyphens are allowed.");
  document.getElementsById('txt_domain').focus();
   return false;
}
  alert("OK This is valid domain");
}
</script>

HTML Code

<html>
 <head>
 <title>Validate Domain Name</title>
 </head>
 <body>
 Your Domain :  <input name="txt_domain" id="txt_domain" type="text" size="30" /> <input name="btn_validate" type="button" value="Validate" onclick="validate_domain()" />
 </body>
</html>

Demo


Your Domain :