Javascript Validation
I am using javascript for validation of forms. When using it for a Text box I have no problem. It doesn't seem to work on a Textarea through.
What part of the following code would I have to change for validating a textarea is not empty. How about a radio button, checkbox, or Drop-Down box?
<?php
<HEAD>
<script language=\"javascript\">
function isFormComplete(){
if(document.theForm.TextBox.value == \"\")
{
alert('You must complete the Text Box');
return false;
}
}
</script>
</HEAD>
<BODY>
<FORM ACTION=\"Submit.asp\" METHOD=\"Post\" NAME=\"theForm\" onSubmit=\"return isFormComplete()\">
<INPUT TYPE=\"Text\" NAME=\"TextBox\">
<INPUT TYPE=\"Submit\"></FORM>
?>
Thank you for any help.
Suzanne posted this at 19:17 — 12th December 2001.
She has: 5,507 posts
Joined: Feb 2000
And so to control elements on the page, you have to work your way down through the layers.
You can use generic terms and numbers to count down, or use the NAME attribute in the field elements to control it, so...
document.theForm.TextBox.value
would be
document.theForm.nameOfField.value
And whatever your checkbox, radio button, et cetera is named replaces nameOfField.
Suzanne
disclaimer: I'm answering the broad question, I didn't look at the actual validity of the JavaScript
artsapimp posted this at 19:30 — 12th December 2001.
They have: 330 posts
Joined: Apr 2000
That part of it is what I thought until I tried it with a textarea. The textarea never seems to equal "".
When I do if(document.theForm.Textarea.value = "") it always disregards the next steps as if it doesn't = "". What am I doing wrong there?
Free Math Test
Fun Math Games
Vincent Puglia posted this at 20:38 — 12th December 2001.
They have: 634 posts
Joined: Dec 1999
Hi artsapimp,
the following works for me (nn4.7, ie6, & should for nn6):
[code]
<script language="javascript">
function isFormComplete(){
if(document.theForm.a.value == "")
{
alert('You must complete the Text Box');
return false;
}
}
</script>
[\code]
so, the question is what does your actual form look like? Insofar as validating all types of fields: see the "Validating Forms" script/tutorial at my site
vinny
Where the world once stood
the blades of grass cut me still
artsapimp posted this at 22:29 — 12th December 2001.
They have: 330 posts
Joined: Apr 2000
No, that also works for me. I am wanting to know how to validate a Radio button, Textarea, Checkbox, etc.
If the textarea's name is Textarea then
document.theForm.Textarea.value == "" is always false. It is never == "" when I try to do it. The same is true for everything except Text boxes.
Is there something else that should be written for each of them?
Free Math Test
Fun Math Games
Suzanne posted this at 03:42 — 13th December 2001.
She has: 5,507 posts
Joined: Feb 2000
Test for true or false for checkboxes and radio buttons.
Suzanne
artsapimp posted this at 15:13 — 13th December 2001.
They have: 330 posts
Joined: Apr 2000
Let me show you what I don't understand. This does not work for me.
<?php
function whatRadio(){
if(document.ManReq.NewOrReplacement.value == \"New\")
{
alert('Yes');
return;
}
else
{
alert('No');
}
}
...
<tr>
<td colspan=\"2\"><input type=\"Radio\" name=\"NewOrReplacement\" value=\"New\" onClick=\"return whatRadio()\">New</td>
<td colspan=\"2\"><input type=\"Radio\" name=\"NewOrReplacement\" value=\"Replacement\"><td>
</tr>
?>
This always alerts 'No'. How would I change this to work?
Free Math Test
Fun Math Games
Vincent Puglia posted this at 15:30 — 13th December 2001.
They have: 634 posts
Joined: Dec 1999
Hi artsapimp,
Re:
reminds of the man who said, "Doc, it hurts when I do this". To which the Doc said, "Don't do it"
Never use reserved words as either variables or their values; you will get away with it once in a while; the rest of the time, you will crash.
Regarding your radio code: Did you check the "Validating Forms" script/tutorial at my site? In it, I state that radio buttons are an array, and have to be treated with the following method:
function whatRadio(fldObj)
{
for (i=0;i< fldObj.length; i++)
if (fldObj[i].checked)
alert(fldObj[i].value)
}
....onClick="return whatRadio(this.form.NewOrReplacement)">New
Note I added an onClick to the second button
Vinny
Where the world once stood
the blades of grass cut me still
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.