validating fields in a text box - how?

HTML, CSS and anything else that deals with client side capabilities.

Moderator: General Moderators

Post Reply
rrn
Forum Commoner
Posts: 46
Joined: Wed Apr 15, 2009 7:54 am

validating fields in a text box - how?

Post by rrn »

hi all ,

in my website there is a page for entering details..

there is a 3 text boxes for entering date of birth

Code: Select all

<td >Date of Birth</td>
    <td ><input type="text" id="date" name="date" value="" class="box-200" /></td> 
<td ><input type="text" id="month" name="month" value="" class="box-200" /></td> 
<td ><input type="text" id="year" name="year" value="" class="box-200" /></td>
 
i have given a code for checking the values entered in the text

Code: Select all

function validate() {
if(document.form.date.value=="")
{
alert ('Please enter date');
return false;
}
if(document.form.month.value=="")
{
alert ('Please enter month.');
return false;
}
if(document.form.year.value=="")
{
alert ('Please enter year.');
return false;
}
}
 
if the text box is left empty it will display an alert message..

but what i need is
date should be a value between 1 and 31. if the value entered is greater than 31, it should display an alert message
likewise month should be between 1 and 12 and year less than 2009..

what change should i make in the code??
please help me to solve this...
Last edited by Benjamin on Mon May 18, 2009 10:22 am, edited 1 time in total.
Reason: Changed code type from text to html, javascript.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: validating fields in a text box - how?

Post by jayshields »

Here's the month one, I'll let you figure out the other two by yourself.

Code: Select all

if(document.form.month.value=="" || document.form.month.value < 1 || document.form.month.value > 12)
{
  alert ('Please enter month.');
  return false;
}
rrn
Forum Commoner
Posts: 46
Joined: Wed Apr 15, 2009 7:54 am

Re: validating fields in a text box - how?

Post by rrn »

Thanks a lot.... it worked....

i have got one more doubt .

i have got a text box for entering email in the same page..

Code: Select all

if(document.form.email.value=="")
{
alert ('Please enter Email.');
return false;
}
the above code is working . if the field is left empty, it will display alert message..

but what i need is , if the user is not entering a valid email id , ie email shhould be in the form abc@sss.com
if not like tat , it should dipsplay message..

what change should i make in the code for that to work??

kindly help pls.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: validating fields in a text box - how?

Post by jayshields »

Search Google for Javascript email validation. There are plenty of regular expressions around for doing just what you want. You could go one better and use jQuery with the validate plug-in.
Post Reply