1 Replies - 542 Views - Last Post: 30 August 2012 - 01:20 AM

#1 Soumava Chakraborty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-May 10

question regarding form validation in javascript

Posted 29 August 2012 - 11:52 PM

<form action='test2.html' >
FirstName:
<input type='text' name='fname'></input></br>
Address:
<input type='text' name='address'></input><br>
Zip:
<input type='text' name='zip'></input><br>
State:
<select name='state'>
<option>Select</option>
<option>Andhra Pradesh</option>
<option>Uttar Pradesh</option>
<option>West Bengal</option>
<option>KARNATAKA</option>
</select><br>
Username(6-8 Characters):
<input type='text' name='uname'></input><br>
Email:
<input type='text' name='email'></input><br>
<input type='submit' value='submit' onclick='return validate()'></input>
</form>
<script type='text/javascript'>
function validate()
{	
var f1=document.forms[0];
var fname=f1.fname;																
var add=f1address;
var zip=f1.zip;
var state=f1.state;
var uname=f1.uname;
var email=f1.email;
if(isAlphabet(fname,"Please enter only Alphabets for your firstname")){
	if(isAlphaNeumeric(add,"Please use only number or Letter for address")){
		if(isNumeric(zip,"please provide a valid numeric zip code")){
			if(stateselection(state,"please select a state")){
				if(validusername(uname,6,8)){
					if(validateemail(email,"please enter a valid email adderss")){
					return true;
					}
				}
			}
		}
	}
}

return false;

}
function notEmpty(sexy,msg)
{
if(sexy.value.length==0)
	{
	sexy.focus();
	alert(msg);
	return false;
	}
return true;
}
function isAlphabet(sexy,msg)
{
var alpha=/^[a-zA-Z]+$/;
if(sexy.value.match(alpha))
	{
	return true;
	}
	else
	{
	sexy.focus();
	alert(msg);
	return false;
	}
}
function isAlphaNeumeric(sexy,msg)
{
var alphaneumeric=/^[0-9a-zA-Z]+$/;
if(sexy.value.match(alphaneumeric))
	{
		return true;
	}
	else
	{
	sexy.focus();
	alert(msg);
	return false;
	}
}
function isNumeric(sexy,msg)
{
var neumeric=/^[0-9]+$/;
if(sexy.value.match(neumeric))
	{
	return true;
	}
	else
	{
	sexy.focus();
	alert(msg);
	return false;
	}
}
function stateselection(sexy,msg)
{
if(sexy.value=="Select")
	{
	sexy.focus();
	alert(msg);
	return false;
	}
	else
	{
	return true;
	}
}
function validusername(sexy,min,max)
{
var userid=sexy.value;
if(userid.length >=min && userid.length <=max)
	{
	return true;
	
	}
	else
	{
	alert("please enter between" + min+" and "+max+"characters");
	sexy.focus();
	return false;
	}
}
function validateemail(sexy,msg)
{
var emailpattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if(sexy.value.match(emailpattern))
	{
	return true;
	}
	else
	{
	sexy.focus();
	alert(msg);
	return false;
	}
}
</script>
------------------------------------------------------------------------------------------
In the above the javascript program is not running successfully.But when I am using the below code it is running well.
_______________________________________________________________________________________________________________
<form action='test2.html' >
FirstName:
<input type='text' id='fname'></input></br>
Address:
<input type='text' id='address'></input><br>
Zip:
<input type='text' id='zip'></input><br>
State:
<select id='state'>
<option>Select</option>
<option>Andhra Pradesh</option>
<option>Uttar Pradesh</option>
<option>West Bengal</option>
<option>KARNATAKA</option>
</select><br>
Username(6-8 Characters):
<input type='text' id='uname'></input><br>
Email:
<input type='text' id='email'></input><br>
<input type='submit' value='submit' onclick='return validate()'></input>
</form>
<script type='text/javascript'>
function validate()
{																								
var fname=document.getElementById('fname');																
var add=document.getElementById('address');
var zip=document.getElementById('zip');
var state=document.getElementById('state');
var uname=document.getElementById('uname');
var email=document.getElementById('email');
if(isAlphabet(fname,"Please enter only Alphabets for your firstname")){
	if(isAlphaNeumeric(add,"Please use only number or Letter for address")){
		if(isNumeric(zip,"please provide a valid numeric zip code")){
			if(stateselection(state,"please select a state")){
				if(validusername(uname,6,8)){
					if(validateemail(email,"please enter a valid email adderss")){
					return true;
					}
				}
			}
		}
	}
}

return false;

}
function notEmpty(sexy,msg)
{
if(sexy.value.length==0)
	{
	sexy.focus();
	alert(msg);
	return false;
	}
return true;
}
function isAlphabet(sexy,msg)
{
var alpha=/^[a-zA-Z]+$/;
if(sexy.value.match(alpha))
	{
	return true;
	}
	else
	{
	sexy.focus();
	alert(msg);
	return false;
	}
}
function isAlphaNeumeric(sexy,msg)
{
var alphaneumeric=/^[0-9a-zA-Z]+$/;
if(sexy.value.match(alphaneumeric))
	{
		return true;
	}
	else
	{
	sexy.focus();
	alert(msg);
	return false;
	}
}
function isNumeric(sexy,msg)
{
var neumeric=/^[0-9]+$/;
if(sexy.value.match(neumeric))
	{
	return true;
	}
	else
	{
	sexy.focus();
	alert(msg);
	return false;
	}
}
function stateselection(sexy,msg)
{
if(sexy.value=="Select")
	{
	sexy.focus();
	alert(msg);
	return false;
	}
	else
	{
	return true;
	}
}
function validusername(sexy,min,max)
{
var userid=sexy.value;
if(userid.length >=min && userid.length <=max)
	{
	return true;
	
	}
	else
	{
	alert("please enter between" + min+" and "+max+"characters");
	sexy.focus();
	return false;
	}
}
function validateemail(sexy,msg)
{
var emailpattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if(sexy.value.match(emailpattern))
	{
	return true;
	}
	else
	{
	sexy.focus();
	alert(msg);
	return false;
	}
}
</script>

----------------------------------------------
why can't we access an element with documents.form[0]; is there any specific reason? I have tried more than 50 times but coudn't get it.

This post has been edited by modi123_1: 29 August 2012 - 11:54 PM
Reason for edit:: highlight the text and click the 'code' button in the style editor..


Is This A Good Question/Topic? 0
  • +

Replies To: question regarding form validation in javascript

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: question regarding form validation in javascript

Posted 30 August 2012 - 01:20 AM

That is because your calls to the document are invalid. Simply giving a variable the value of the whole document then using an id of an element as the variable name will not work, as it will assume that the document has a native variable of your id names. Why not use document.getElementById?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1