Membership is FREE, giving all registered users unlimited access to every Acorn Domains feature, resource, and tool! Optional membership upgrades unlock exclusive benefits like profile signatures with links, banner placements, appearances in the weekly newsletter, and much more - customized to your membership level!

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.

The Rule #1

Do not insult any other member. Be polite and do business. Thank you!

Members online

Premium Members

New Threads

Domain Forum Friends

Our Mods' Businesses

*the exceptional businesses of our esteemed moderators
General chit-chat
Help Users
  • No one is chatting at the moment.
      There are no messages in the current room.
      Top Bottom