Membership is FREE – with unlimited access to all features, tools, and discussions. Premium accounts get benefits like banner ads and newsletter exposure. ✅ Signature links are now free for all. 🚫 No AI-generated (LLM) posts allowed. Share your own thoughts and experience — accounts may be terminated for violations.

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.

Rule #1: Be Respectful

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.
  • Siusaidh AcornBot:
    Siusaidh has left the room.
      Siusaidh AcornBot: Siusaidh has left the room.
      Top Bottom