...

View Full Version : checking to determine if multiple fields are blank



emkay717
04-20-2009, 01:19 AM
I am trying to validate a form. In one case at least one of 4 fields must have a value. I have an isBlank() function that determines if a field is blank. Now I want to test all 4 and throw up an error if they are all blank. Here is what I have ...


function isBlank(str)
{
if (str != null)
{
for (i=0;i<str.length;i++)
{
if (str.charCodeAt(i) >= 33)
return false;
}
}
return true;
}


if ((isBlank(txtEvent1.value)) && isBlank(txtEvent2.value)) && isBlank(txtEvent3.value)) && isBlank(txtEvent4.value)))
sErr += 'You must enter at least one ticketing event.\n'

I have tried adding & removing parentheses .. I can't figure out the syntax error. Thanks for any help!

Philip M
04-20-2009, 11:52 AM
Try this:-


<script type = "text/javascript">

var count = 0;
function isBlank(str) {
str = str.replace(/^\s+|\s+$/g,""); // strip leading and trailing spaces
str = str.replace(/[^A-Za-z0-9\s]/g,""); // strip all but alphanumeric and whitespace
if (str.length < 1) { // blank field
count ++;
}
if (count == 4) { // all four fields blank
count = 0;
return false; // i.e. validation failed
}
return true;
}

if ((isBlank(txtEvent1.value)) && (isBlank(txtEvent2.value)) && (isBlank(txtEvent3.value)) && (isBlank(txtEvent4.value)))
sErr += 'You must enter at least one ticketing event.\n'

</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum