Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

text box validation using OR

Status
Not open for further replies.

jwpward

Programmer
Sep 5, 2003
45
GB
I need to do some verification on a 'title' text box in an html form. It needs to adhere to either Mr, Mrs, Miss etc.
I was going to use something like this...

if (Title.value != ("Mr" || "Mrs" || etc)){
alert("Invalid Title. Must be either Mr, Mrs, Ms, Miss, Dr, Prof, Rev, Sir, Lady, Lord");
return;
}

...but it's a bit messy and the boolean OR doesn't seem to work. Maybe i should put all the values in an array, and then do the validation on that?
Any help would be much appreciated.
Cheers
p.s. i can't use a drop down list due to data reasons
 
If you can't use a dropdown selection box, then the array would be my recommendation:

Code:
function ValidateTitle() {
    var titles = new Array("Mr", "Mrs", "Ms", "Miss", "Dr", "Prof", "Rev", "Sir", "Lady", "Lord");
    var titleValid = false;
    var theTitle = document.forms[0].title.value;
    for(var x=0; x<titles.length; x++) {
        if(theTitle == titles[x]) {
            titleValid = true;
        }
    }
    if(!titleValid) {
        alert(&quot;Invalid Title. Must be either: Mr, Mrs, Ms, Miss, Dr, Prof, Rev, Sir, Lady, or Lord&quot;);
        return false;
    }
    return true;
}

NOTE: This won't check for case mismatches.

IMHO - I'd go for the SELECT dropdown list.

Hope this helps.

Pete.


Web Developer / Aptrix CMS (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Woops... used a reserve name for my field name :-(

Try this instead:
Code:
<html>
<head>
	<title>User Title...</title>
<script language=&quot;JavaScript&quot;>
function ValidateTitle() {
    var titles = new Array(&quot;Mr&quot;, &quot;Mrs&quot;, &quot;Ms&quot;, &quot;Miss&quot;, &quot;Dr&quot;, &quot;Prof&quot;, &quot;Rev&quot;, &quot;Sir&quot;, &quot;Lady&quot;, &quot;Lord&quot;);
    var titleValid = false;
    var theTitle = document.forms[0].usertitle.value;
    for(var x=0; x<titles.length; x++) {
        if(theTitle == titles[x]) {
            titleValid = true;
        }
    }
    if(!titleValid) {
        alert(&quot;Invalid Title. Must be either: Mr, Mrs, Ms, Miss, Dr, Prof, Rev, Sir, Lady, or Lord&quot;);
        return false;
    }
    return true;
}
</script>
</head>
<body>
<form onsubmit=&quot;return ValidateTitle();&quot;>
	<input type=&quot;text&quot; name=&quot;usertitle&quot;>
	<input type=&quot;submit&quot;>
</form>
</body>
</html>

Pete.


Web Developer / Aptrix CMS (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Thanks for your help.

The titles need to be in that exact case, so case mismatching isn't an issue. That should sovle it perfectly.

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top