User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 455,973 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,819 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 1391 | Replies: 4
Reply
Join Date: Nov 2007
Posts: 99
Reputation: kings is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kings's Avatar
kings kings is offline Offline
Junior Poster in Training

Help validate form with javascript

  #1  
Nov 30th, 2007
hi,
i am new to java script but my code is not working...please do tell the error in my code

<script language="javascript">
function validateform() {
valid=true;
for (var i=0; i < document.userdetails.elements.length; i++) {
var element = document.userdetails.elements[i];
switch (element.name){
case "cname":
if (element.value.length==0) valid=false;
break;
case "address":
if (element.value.length==0) valid=false;
break;

}

}
if (valid) {
respons = confirm("Do you want to proceed?");
if (respons) return true;
else return false;
} else {
alert ("Please fill in all required fields marked with *");
return false;
}
return false;
}
</script>
<script language="javascript" src="utils.js"></script>
</head>
<body>

<form method="post" action="care1.php" onSubmit="return validateform();">
<table>

<tr>
<td>Company Name </td>
<td><input type="text" name="cname" size="53"></td>
<td><span style="color:red;">*</span></td>
</tr>
<tr><td>Address </td>
<td><TEXTAREA NAME="caddr" COLS=40 ROWS=3></TEXTAREA></td>

<td><span style="color:red;">*</span></td>
</tr>
<td><input type="submit" name="Submit" value="Submit"></td>

</tr>
</table>
</form>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 7,009
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 25
Solved Threads: 368
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: validate form with javascript

  #2  
Nov 30th, 2007
What do you mean by 'not working'? We have no idea what you are trying to exactly achieve here? Are you testing this application in Firefox? If no, then you should start using it. If yes, then look at the error console.

From what I can see, you are accessing the elements of a form named 'userdetails' but I don't see that name given to your form element. Other than that, you need to provide us more details.

Next time post the code with code tags as mentioned in the announcements and site rules.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: validate form with javascript

  #3  
Dec 6th, 2007
Alright. You need to check these 2 things.

1. The form has no name. name = "userdetails" is missing.
2. Textarea name is caddr, but you are checking for address.

Other than that, I dont see anything wrong. This should work.

Cheers,
Naveen
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Dec 2007
Location: Russia
Posts: 11
Reputation: adorosh is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
adorosh adorosh is offline Offline
Newbie Poster

Troubleshooting Re: validate form with javascript

  #4  
Dec 6th, 2007
You have three bugs man:
1. Fill name of form, set
<form name="userdetails" method="post" ... >
2. Where you verify whether the fields are empty, change 2nd "case" statement to "caddr":
  1. case "caddr":
  2. if (element.value.length==0) valid=false;
  3. break;
3. In the beginning of function, where you define the valid variable set key word "var":
  1. var valid = true;
This bug is actual for IE users only.

Good luck.
Last edited by adorosh : Dec 6th, 2007 at 1:56 pm.
Reply With Quote  
Join Date: Dec 2007
Posts: 75
Reputation: hielo is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
hielo hielo is offline Offline
Junior Poster in Training

Re: validate form with javascript

  #5  
Dec 8th, 2007
Rather than checking each of the required input elements within the switch, you can simply provide a css class to each of the required elements. EX:
<input type="text" name="first" class="required" value=""/>

Then, your script will check each of the form elements for the existence if this "required" class and if so, then you check if it was actually filled.

Here is your modified code in it's entirety. Pay attention how I specified the required fields in the form elements rather than on the script.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script language="javascript">
function validateform() {
valid=true;
var f = document.getElementById('userdetails');
for (var i=0; i < f.elements.length; i++)
{
var element = f.elements[i];
if( /\brequired\b/i.test( element.className ) && !String(element.value).length )
valid=false;
}
if (valid)
{
respons = confirm("Do you want to proceed?");
if (respons)
return true;
else
return false;
}
else
{
alert ("Please fill in all required fields marked with *");
return false;
}
return false;
}
</script>
<script language="javascript" src="utils.js"></script>
</head>
<body>

<form id="userdetails" method="post" action="care1.php" onSubmit="return validateform();">
<table>

<tr>
<td>Company Name </td>
<td><input type="text" class="required" name="cname" size="53"></td>
<td><span style="color:red;">*</span></td>
</tr>
<tr><td>Address </td>
<td><TEXTAREA NAME="caddr" COLS="40" ROWS="3" class="required" ></TEXTAREA></td>

<td><span style="color:red;">*</span></td>
</tr>
<td><input type="submit" name="Submit" value="Submit"></td>

</tr>
</table>
</form>

</body>
</html>

You are welcome!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 9:15 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC