•
•
•
•
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 397,574 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,471 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: 1243 | Replies: 4
![]() |
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>
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>
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.
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."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 238
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
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*
*PM asking for help will be ignored*
•
•
Join Date: Dec 2007
Location: Russia
Posts: 11
Reputation:
Rep Power: 1
Solved Threads: 1
You have three bugs man:
1. Fill name of form, set
2. Where you verify whether the fields are empty, change 2nd "case" statement to "caddr":
3. In the beginning of function, where you define the valid variable set key word "var":
This bug is actual for IE users only.
Good luck.
1. Fill name of form, set
<form name="userdetails" method="post" ... >
javascript Syntax (Toggle Plain Text)
case "caddr": if (element.value.length==0) valid=false; break;
javascript Syntax (Toggle Plain Text)
var valid = true;
Good luck.
Last edited by adorosh : Dec 6th, 2007 at 12:56 pm.
•
•
Join Date: Dec 2007
Posts: 75
Reputation:
Rep Power: 1
Solved Threads: 10
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!
<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!
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
- Send a text message to your phone from your site! (ColdFusion)
- javascript code not working in firefox (JavaScript / DHTML / AJAX)
- PHP: Form Validation (PHP)
- PHP Contact Form Help (PHP)
- Why javascript does not work on firefox? (JavaScript / DHTML / AJAX)
- How to validate an image field with javascript (JavaScript / DHTML / AJAX)
- PHP Quote form (PHP)
- Javascript problem in FIREFOX (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: disable parent window
- Next Thread: Can you?



Linear Mode