PDA

View Full Version : javascript: checking different parameters of form validation



nischalinn
04 Jun 2011, 12:23 PM
I've created a form.
**********************************************************************
<form id="form1" method="post">
<table border="0">
<tr>
<td>User Id</td>
<td><input type="text"name="User Id" id="UserId" /></td>
</tr>

<tr>
<td>Date</td>
<td><input type="text"name="DateOfReg" id="DateOfReg"/></td>
</tr>

<tr>
<td>E-mail Address:</td>
<td><input type="text"name="E-mail Address:" id="Email"/></td>
</tr>

<tr>
<td>Name:</td>
<td><input type="text"name="NameOfUser" id="NameNameOfUser"/></td>
</tr>

<tr>
<td><input type="image" src="SUBMIT.gif" id="SubmitButton"/></td>
<td><input type="image" src="RESET.gif" id="ResetButton"/></td>
</tr>
</table>
</form>
*******************************************************************
Now I want to write javascript code for the processes inside the HTML file:
1. check for the condition if any of the cell is NULL, if any one of the cell is NULL give an alert message.
2. validation of e-mail address, like it must contain atleast one '@' 'dotcom(.com)'
3. check the length of the name input

I am learning javascript and not getting idea how to perform this task. Will anyone help me???

MarPlo
09 Jun 2011, 08:07 AM
Hy,
With the following function you can check those conditions


function SubmitForm(id) {
var objid = document.getElementById(id).value;
if( (objid==NULL || objid.length<3 || objid.indexOf("@")==-1 || objid.indexOf('.')==-1) {

alert("please complete email address corectly");
return false;
}
else return true;
}

If you are learning JavaScript, study the code and adapt it for youre use.
You need to create more if() conditions in that function, one separately for mail field.