...

View Full Version : Error in validating null fields...



zil
07-13-2004, 02:38 PM
Hello everyone!
Im trying a simple validation...i have 3 fields and if ever one of the 3 fields is not empty, you need to fill all the 3 fields...in fact its about an application form where a person needs to fill in his Bachelor Degree Title, Grade and University...if ever the person enter his/her Bachelor Title, the person needs to enter a grade and the University attended and so on...well my codes are as follows:

<script language="javascript">
function checkDegree(form)
{
var dTitle, dGrade, dUniv

dTitle = form.B_Degree_Title;
dGrade = form.B_Degree_Grade;
dUniv = form.B_Degree_Univ;

if (dTitle.value != null && dGrade.selectedIndex == 0)
{
alert ("Select a grade for your Bachelor's Degree please.");
return (false);
}
else if (dTitle.value != null && dUniv.value == null)
{
alert ("Fill in a a value for the University field for your Bachelor's Degree please.");
return (false);
}
else if (dGrade.selectedIndex > 0 && dTitle.value == null)
{
alert ("Fill in a a value for the Title field for your Bachelor's Degree please.");
return (false);
}
else if (dGrade.selectedIndex > 0 && dUniv.value == null)
{
alert ("Fill in a a value for the University field for your Bachelor's Degree please.");
return (false);
}
else if (dUniv.value != null && dTitle.value == null)
{
alert ("Fill in a a value for the Title field for your Bachelor's Degree please.");
return (false);
}
else if (dUniv.value != null && dGrade.selectedIndex == 0)
{
alert ("Select a grade for your Bachelor's Degree please.");
return (false);
}
}
</script>

Now the problem is that i have a link and as the person clicks on the link, the validation should run...but im having this error message:

Error:'B_Degree_Title' is null or not an object

The B_Degree_Title is the name of the title text box. Can anyone help me please i would be very grateful, thx.

_______________
Zil.

glenngv
07-13-2004, 02:55 PM
Is the case correct? How are you passing the form argument to the function? And you shouldn't check for null values like

if (dTitle.value != null && ...

as field values will never be null. You want to check for empty not null.

if (dTitle.value != "" && ...

jbot
07-13-2004, 02:56 PM
Error:'B_Degree_Title' is null or not an object

The B_Degree_Title is the name of the title text box.

not according to the document, it isn't. hence the error. your script can't find the element, hence it thinks it's not an object.

check the spelling first.

second, what it the value of "form" being passed to checkDegree?

zil
07-13-2004, 03:03 PM
Yes in fact ive also check for null fields as B_Degree_Title = "" and the same error occurs...the form paramater will normally be the name of my form, in that case degree (<form ACTION="" METHOD="POST" name="degree">)...im calling the function as follows:
<a href="" onMouseDown="checkDegree(this.form)"> Save and Continue</a>

btw, something seems very strange, if i call the function from an input text box through the Onclick event, the validation works. For e.g.
<input type="text" name="Test" onClick="checkDegree(this.form)"></input>
The above way the validation works but i can unfortunately not valid a form likewise.
____________
Zil.

Kor
07-13-2004, 03:33 PM
<a href="" onMouseDown="checkDegree(this.form)"> Save and Continue</a>

wount work, and it is not weird at all. this.form is a substitute for form object to be use only in relationship with the form's elements (input, textarea, select, etc). But <a> is a simple tag, not a form's element

Use the full reference and you will get rid of problems

function checkDegree()
{
var dTitle, dGrade, dUniv

dTitle = document.forms['form_name'].B_Degree_Title;
dGrade = document.forms['form_name'].B_Degree_Grade;
dUniv = document.forms['form_name'].B_Degree_Univ;
...
...
and

<a href="#" onMouseDown="checkDegree()"> Save and Continue</a>

zil
07-14-2004, 06:37 AM
Kor: Oh i c!!!im gonna try it now...hey guys ur really fantastic!!:)
Thx a lot mates!!

_______________
Zil.

zil
07-14-2004, 07:00 AM
Kor: Hey it really does work!!!!!
I have been struggling on that for hours...thx a lot.
Thx to others 2 coz uve been helping a lot 2, eventhough through small comments, that's largely great help for me:D

_____________
Zil.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum