Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript validation: Trying to set a blank value to zero

Status
Not open for further replies.

tokuzumi

Programmer
Sep 16, 2002
40
US
I have a form, broken down into columns of varying numbers of text boxes. The columns cannot total to greater than 100%. But all fields are not required. Here is how I do my validation:

function hiddenForm_OnSubmit()
{
var returnValue;
if ((document.form.fieldname.value > 100) || (document.form.fieldname.value < 0))
{
alert(&quot;Fieldname cannot be less than 0, or greather than 100&quot;);
returnValue = false;
form.fieldname.focus;
}
else if......and it goes on from there, with the same format as above. When I try to add the snippet of code below, I get an error that says &quot;Object Expected&quot;, and a &quot;Syntax Error&quot;. I want to make sure that if a field is not filled in, that JavaScript considers it a zero, so when I add the column together, if a value in the column has no value, JavaScript will think it's a big ole zero.
if ((document.form.field1.value == &quot;&quot;) || (document.form.field1.value == null))
{
var field1 = &quot;0&quot; ;}
else {
var field1 = (document.form.field1.value);}

Thanks for any help.
 
i'm unsure about what values you want but have a look at this it might do what you want or you can adapt it.

Hope it helps
 
how about running a loop throguh all the fields prior to running this condition to set empty val's to 0
for (i = 0; i <= MyFrom.length; i++)
{
if(MyForm.field.value==&quot;&quot;)
MyForm.field.value=0

etc..

then you'll be able to test as a int

____________________________________________________
The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811

onpnt2.gif
 
it might help if i sent the code!

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>

<html>
<head>
<title>Untitled</title>

<script>
function hiddenForm_OnSubmit(){

var myForm = document.f
for(var i = 0; i < myForm.length; i++ ){
var e = myForm.elements;

if( ( e.type == &quot;text&quot;) && ( e.value > 100 ) || ( e.value < 0 ) ) {
alert('if');
return false;
}
else if( (e.value == null) || (e.value == &quot;&quot;) ){
alert('else');
e.value = 0;
}
}
return false;
}


</script>

</head>

<body>
<form name=&quot;f&quot; action=&quot;&quot; onsubmit=&quot;return hiddenForm_OnSubmit();&quot;>
<input type=&quot;text1&quot; value=&quot;&quot;>
<input type=&quot;text2&quot; value=&quot;&quot;>
<input type=&quot;submit&quot;>
</form>
 
the value will never be null.
You could also include the adding script with the looping script etc...

function hiddenForm_OnSubmit(){
var col1=0;
var col2=0;
var myForm = document.f
for(var i = 0; i < myForm.length; i++ )
{
var e = myForm.elements;

if( e.type == &quot;text&quot;)
{
var ename = e.name;
//alert(ename)
val = Number(e.value)
if (ename.indexOf(&quot;Col1&quot;)==0) {
if (( val > 100 ) || ( val< 0 )) {alert(&quot;Fieldname cannot be less than 0, or greather than 100&quot;); e.focus(); return false; break}
else {col1+=val}
}
else if (ename.indexOf(&quot;Col2&quot;)==0) {
if (( val > 100 ) || ( val < 0 )) {alert(&quot;Fieldname cannot be less than 0, or greather than 100&quot;); e.focus(); return false; break}
else {col2+=val}
}

}
}
alert(&quot;Col1 Total=&quot;+col1);
alert(&quot;Col2 Total=&quot;+col2);
}
 
In addition to the last post. For the script to work I created 2 columns each with input text boxes in them. in the first column I prefixed the textbox names with 'Col1' and in the second column with 'Col2'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top