Quick question about checking a field for empty (if/else - redirect)

I think this is a really quick question - and would appreciate any help…

I have the following simple form vaidation script on a [classic] ASP page:

<script>
	function validateForm(frm)
		{
		if(frm.subject.value=="")
		{
		alert('You must provide a subject.');
		frm.subject.focus;
		return false;
		}
		if(frm.message.value=="")
		{
		alert('You must provide a message.');
		frm.message.focus;
		return false;
		}
	frm.submit();
	}
</script>

I want to add one more validation
at the beginning of the script,
just to slow down spam bots:

For the following input field:

<input type="text" name="foo" style="display: none;">

The validation should:

if [foo] equals "" contunue to the rest of the validation
else
if [foo] doesn't = "" redirect to [http://foo.com/page.asp]

Getting the script syntax right is driving me nuts -
can you help?

Thanks,

  • Michael
    (obviously not a coder)

Looks more like JScript to me… I think you’re looking for something like this (no guarantees though, I haven’t tested it):


<script>
	[COLOR=#E56717][B]function[/B][/COLOR] validateForm(frm)
		{
		[COLOR=#8D38C9][B]if[/B][/COLOR](frm.subject.value==[COLOR=#800000]""[/COLOR])
		{
		alert([COLOR=#008000]'You must provide a subject.');
[/COLOR]		frm.subject.focus;
		return [COLOR=#00C2FF][B]false[/B][/COLOR];
		}
		[COLOR=#8D38C9][B]if[/B][/COLOR](frm.message.value==[COLOR=#800000]""[/COLOR])
		{
		alert([COLOR=#008000]'You must provide a message.');
[/COLOR]		frm.message.focus;
		return [COLOR=#00C2FF][B]false[/B][/COLOR];
		}
                if (frm.foo.value != "" || frm.foo.length != 0) {
                response.redirect('foo.asp');
                }
	frm.submit();
	}
</script>

I couldn’t get it to work exactly the way you described:
if (frm.foo.value != “” || frm.foo.length != 0)
The form wouldn’t submit (or maybe just didn’t validate?)

But when I eliminated the || check for !=0 like this:
if (frm.foo.value != “”)
it appears to work. Thank you!

(any idea on how to add back in the check for !=0 ??)

Again - Thanks for your help.

Can you show us more of the page please? Specifically your code In Context with the rest of the page, including your HTML form? (Delete or mask anything sensitive or personal).

(To me it looks like you’re trying to mix Javascript and ASP).