Forum Moderators: open

Message Too Old, No Replies

empty validation

what is wrong with my empty validation function?

         

swati

10:37 am on Dec 10, 2004 (gmt 0)

10+ Year Member



What is wrong with the following function. When i click ok in the alert box the form should not get submitted but it does.
function emptyValidation(entered,alertbox){
with(entered){
if(value==null¦¦value==""){
if(alertbox!=""){
alert(alertbox);
}
return false;
} else{ return true;}
}
}

<form name="myForm" method="get" onsubmit="return emptyValidation(usage,"Please enter a value!")">
<input type="text" name="usage">
<input type="submit" name="go" value="go">
</form>

Bernard Marx

10:53 am on Dec 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Possible probs:

1. Bad quote nesting.
2. Reference, usage, enough.

Try:

onsubmit="return emptyValidation(this.usage,'Please enter a value!')"

I don't like 'with', it's confusing. How about:

function emptyValidation(entered,alertbox)
{
var value = entered.value;
if(!value)// empty string is false
{
if(alertbox)
alert(alertbox);
return false;
}
else return true;
}

// or // simply..

function emptyValidation(entered,alertbox)
{
if(alertbox &&!entered.value) alert(alertbox);
return!entered.value;
}

Oh the online editor again!
The line line should read:

return[space][excl!][excl!]entered.value;

swati

11:14 am on Dec 10, 2004 (gmt 0)

10+ Year Member



No its still not working, here s the code:(i m using mozilla firefox browser)
<script type="text/javascript" language="JavaScript">
function emptyValidation(entered,alertbox){
var value = entered.value;
if(!value)// empty string is false
{
if(alertbox)
alert(alertbox);
return false;
}
else return true;
}
</script>

<form name="myForm" method="get" onsubmit="return emptyValidation(this.usage,"Please enter a value!")">
<input type="text" name="usage" size=3>
<input type="submit" name="go" value="go">
</form>

swati

11:16 am on Dec 10, 2004 (gmt 0)

10+ Year Member



hey no no its working , i did not put the right quotes around 'please enter a value'
It works now.
Thank you so much....this is the best forum in the world!

rocknbil

6:10 pm on Dec 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually this poses a good question (ok, maybe an old one :D ) - all coding errors aside, why doesn't an obviously blank value evaluate as null or 'null' or '' sometimes?