Form validation using jQuery

August 25th, 2010

Form validation using jQuery.

jQuery has a plugin for form validation, you can download this plugin from

http://bassistance.de/jquery-plugins/jquery-plugin-validation/

Here are the steps to add Jqury form validation on your page.

Step 1 – Download jQuery library and jQuery form validation plugin

    • JQuery (Can be downloaded from http://jquery.com/ )
    • JQuery form validation plug-in ( Can be downloaded from http://bassistance.de/jquery-plugins/jquery-plugin-validation/)
    • After downloading, you will have to include these two files in your HTML page under head tag

      <script type="text/javascript" src="jquery.js"></script>
      <script type="text/javascript" src="
      jquery.validate.js"></script>

      we assume these two files are in same directory where your page exist.

  • We need two things

Step 2 – The page on which validation is to be perform

  • Create a page, for example test.html.
  • Now create the form, for example.

    <form id=“form1″ method=“post” action=“”>

<div class=“form-row”>

<span class=“label”>Name *</span>

<input type=“text” name=“name” />

</div>

<div class=“form-row”>

<span class=“label”>E-Mail *</span>

<input type=“text” name=“email” />

</div>

<div class=“form-row”>

<span class=“label”>URL</span>

<input type=“text” name=“url” />

</div>

<div class=“form-row”>

<span class=“label”>Your comment *</span>

<textarea name=“comment” ></textarea>

</div>

<div class=“form-row”>

<span class=“label”>Password *</span>

<input type=text name=“password” />

</div>

<div class=“form-row”>

<input class=“submit” type=“submit” value=“Submit”>

</div>

    </form>

Now we have created a simple form, which we will validate.

Note: The two jQuery files need to be included in this page.

Step 3 – The Complete page with JQuery form validation Plugin

<html>

<head>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.validate.js"></script>

<script type=”text/javascript”>

//Here Form validation realated operation will be done

//initiate validator on load

$(document).ready(function() {

$(”#form1″).validate({

rules: {

name: “required“,// simple rule, converted to {required:true}

email: {// compound rule

required: true,

email: true

},

url: {

url: true

},

comment: {

required: true

},

password:{

required: true

minlength:5 //Password minimum length would be 5 characters

}

},

messages: { //Custom Error/Validation Message goes here, if this will not be given then the default error/validation message will be shown

name: “You must enter the name”,

comment: “Please enter a comment.”

}

});

});

</script>


<style type="text/css">

* { font-family: Verdana; font-size: 11px; line-height: 14px; }

.submit { margin-left: 125px; margin-top: 10px;}

.label { display: block; float: left; width: 120px; text-align: right; margin-right: 5px; }

.form-row { padding: 5px 0; clear: both; width: 700px; }

label.error { width: 250px; display: block; float: left; color: red; padding-left: 10px; }

input[type=text], textarea { width: 250px; float: left; }

textarea { height: 50px; }

</style>

</head>

<body>

<form id=“form1″ method=“post” action=“”>

<div class=“form-row”>

<span class=“label”>Name *</span>

<input type=“text” name=“name” />

</div>

<div class=“form-row”>

<span class=“label”>E-Mail *</span>

<input type=“text” name=“email” />

</div>

<div class=“form-row”>

<span class=“label”>URL</span>

<input type=“text” name=“url” />

</div>

<div class=“form-row”>

<span class=“label”>Your comment *</span>

<textarea name=“comment” ></textarea>

</div>

<div class=“form-row”>

<span class=“label”>Password *</span>

<input type=text name=“password” />

</div>

<div class=“form-row”>

<input class=“submit” type=“submit” value=“Submit”>

</div>

</form>

</body>

</html>

Description:---

All we are doing here is calling an onload function, which initiates the validation of the form using validate. The validate plugin requires you pass through a few paramenters.

Parameter details used in this plugin:—-

rules – allows you to specify the field names you wish to act upon, in our case we have used name, email, url, comment and password. Inside of this we are specifying if the the field is required or not, by passing either true or false, we want to validate all fields, so all are marked as true. The next parameter is minlength, which ..you guessed it, allows you to set a minimum number of characters to be entered.

message – allows you to set a specific message for a particular field, if you choose not to set a message, a default one is provided which will say something like “this field is required”

List of built-in Validation methods

  • required()
    Makes the element always required.
  • minlength(length)
    Makes the element require a given minimum length.
  • maxlength(length)
    Makes the element require a given maximum length.
  • min(value)
    Makes the element require a given minimum value.
  • max(value)
    Makes the element require a given maximum.
  • range(range)
    Makes the element require a given value range.
  • Email()
    Makes the element require a valid email
  • url()
    Makes the element require a valid url
  • date()
    Makes the element require a date.
  • equalTo(other)
    Requires the element to be the same as another one
  • digits()
    Makes the element require digits only.


Alert Special symbols of unicode e

April 5th, 2010

Alert the message of different language with special symbol .

If you are facing the problem show the message in the alert box which contain the special character like your string is in French ie. “Aménagement du logement”.for this you are using javascript code alert(”Aménagement du logement”).
AS well as you are using dynamic message for the alert box ie. you are not sure that on which position and how many time special symbol will occur in your message .

Then you need a method as follows with following steps.

1. You have to defined the the string with following format
“Am&#233nagement du logement” where I replaced é with the &#233, ie. replace special character with the equivalent code.

2.copy and pest following method in your script tag

function checkSpecialCharecter(data){
var message=”";
var pos=0;
var res=0;
var iChars = “!@#$%^&*()+=-[]\\\’;,./{}|\”:

<>?~_” ;
for (var i = 0; i < data.length; i++) {
if (iChars.indexOf(data.charAt(i)) != -1) {
var temp=data.charAt(i+2)+data.charAt(i+3)+data.charAt(i+4)
temp=String.fromCharCode(temp);
data1=data.substr(pos,i-pos);
message=message+data1+temp;
pos=i+5;
i=i+5;
}

}
if(pos >= data.length) {
message=message;
}else {
message=message+data.substr(pos,data.length-pos);
}
return message;
}

3. you want to alert once  for different validation then.

You have to call the above method in following format where I consider that there is an array of named language which get the dynamic message on run time basis of selected language of site. Now you will get the dynamic message of different language (like English ,French etc.)

if(document.adsForm.age.value == “”){
flag=true;
valid = false;
var codedMessage=checkSpecialCharecter(language["age"]);
msg=msg + codedMessage+ “,”;
}

if(document.adsForm.title.value == “”){
flag=true;
valid = false;
var codedMessage=checkSpecialCharecter(language["title"]);
msg=msg + codedMessage+ “,”;
}

if(flag ==true)
{
alert(msg);
}

This alert will show you the message as you defined in the language array here you can also use string on the place of language variable.