PDA

View Full Version : clearing a value from a form field


hendryk
09-01-2002, 10:02 PM
Hi,

I have code that checks to see that all mandatory fields have been filled. How do I get it to clear that field. At the moment the error message is generated but after clicking OK the value is still accepted and the user can go onto the next field.

Here is the code:

for (i = 0; i<fieldArray.length;i++)
{
field =eval("aform." + fieldArray[i]);
if(field.value =="")
{
fieldName = userNames[i];
decision = false;
break;
}
}
for (i = 0; i<combo.length;i++)
{
field = eval("aform." + combo[i]);
if(!checkComboBox(field))
{
fieldName = comboUser[i];
decision = false;
}
}

if (!decision){alert(fieldName + " is a required field. Please enter a value.");}
else { aform.submit(); prompt = "The form is being submitted.";alert(prompt);}

}

The arrays etc are a bit long - didn' include them.

Thanks

Katie

mordred
09-02-2002, 02:45 PM
Originally posted by hendryk
I have code that checks to see that all mandatory fields have been filled. How do I get it to clear that field. At the moment the error message is generated but after clicking OK the value is still accepted and the user can go onto the next field.


I'm not sure what you're trying to achieve: In your code, you check whether a form field consists of an empty string - which is more or less the same as "not filled". And if that's the case, you want to clear that field? Huh?

Apart from that, it might depend on how you call your validating function. Furthermore, it would be interesting to know what's in checkComboBox(), if that's where the problem lies. But without knowing your form structure and the other js code, it's really hard to say.

Just one tip more: Try to get rid of the eval() statements. In your particular case they are useless, just consuming computing resources, and can be easily substituted by element lookup notation, like

document.aform.elements[fieldArray[i]];

hendryk
09-02-2002, 02:59 PM
Hi

Sorry, I was a bit confused when I did the last post.

What I mean't to ask was:

I have code that checks that a value entered into a particular field is in the correct range. This works fine and a alert box appears. However the invalid value still remains in the field after tyhe ok button has been clicked and the user can carry on filling the form. How do I clear the invalid value and not let the user continue until it is valid.

Here is the function:

function verifyBirthWeightValue(bwt,aform)
{ var decision = true;
var prompt = "";
if ((bwt<1)||(bwt>5))
{ prompt = "Birth Weight must be between 1kg and 5kg."; decision = false; }
if (!decision) { alert(prompt); }
}


Thanks

Katie

D2K2
09-02-2002, 03:35 PM
How about this? (I've made up a form, just change "name=...")

<form name="form1">
Age:<input type="text" name="age" onBlur="checkage()">
</form>


<script>
function checkage(){
var valid="nope"
var age=document.form1.age.value
if (age > 0){
if (age <= 150){
var valid="yep"
}
}
if (valid !== "yep"){
document.alert("The Age must be bettween 1 and 150.")
document.form1.age.value=""
document.form1.age.focus()
}
}
</script>


The Focus and Blur are when the user clicks away from the box, onto the next box, or onto a link, etc.
You can also prevent a user from clicking a box by using:
<input type="text" name="noclick" onFocus="document.form1.noclick.blur()">