• Welcome to AcornDomains.co.uk, a friendly community for UK domain investors, developers and anyone interested in domain names.

    Whether you're looking to buy or sell domains, learn more about the UK domain market, get feedback on your names, or simply chat with other domainers, you're in the right place. AcornDomains has been bringing together people from across the UK domain industry for many years, from complete beginners to experienced investors.

    Have a look around, join the discussions and, if you haven't already, create a free account and introduce yourself.

javascript check form

Status
Not open for further replies.
Joined
Mar 14, 2007
Posts
396
Reaction score
2
I've got a bit of javascript to to check a field in a form on my web page.

It's a competition so I need to make sure the visitor types it in correctly.

the javascript to check the 'answer' field is

Code:
 if (form.answer.value != "brabantia") {	
    alert( "oops! try again?" );
    form.answer.focus();	
    return false ;
  }
  return true ;
}

but this is case sensitive.

I'd like to add some kind of other check for "Brabantia" too.

The javascript needs to return true whether the visitor enters 'brabantia' or 'Brabantia'

Any help?

Phil
 
PHP:
if (form.answer.value != "brabantia" && form.answer.value != "Brabantia") {	
    alert( "oops! try again?" );
    form.answer.focus();	
    return false ;
  }
  return true ;
}

Just modifying the if condition to check for both...

Probably I would use match() http://www.w3schools.com/jsref/jsref_match.asp with a case-insensitve regular expression.
 
Thanks JDubya

I haven't tried that yet but I did solve the problem

Instead of the javascript checker checking for lower and upper case I simply converted any uppercase to lower first in the html form on the page.

I used...

Code:
onChange="javascript:this.value=this.value.toLowerCase();"
 
PHP:
if (form.answer.value != "brabantia" && form.answer.value != "Brabantia") {	
    alert( "oops! try again?" );
    form.answer.focus();	
    return false ;
  }
  return true ;
}

Thanks for this it works fine.

Another bit of useful code snippet to add to my library.

Cheers
 
Status
Not open for further replies.
Top Bottom