/    Sign up×
Community /Pin to ProfileBookmark

does text box contains a word or phrase

I have a tricky text box problem.

I need to check the text box to see if the user input specific words. I know how to check the text box to see if it EQUALS a specific string but I’m not sure how to check if it CONTAINS a string.

I need to see if the checkbox

either contains the words:
“checking” or “savings”
and if it does, send the user to pageA.htm

or if it contains the phrases:
“account number” or “bank number” or “transit number”
and if it does, send the user to pageB.htm

function checkform(){
if (form.userResponse.value ????){
window.document.location = “pageA.htm”}
else if (form.userResponse.value ????){
window.document.location = “pageB.htm”;}
else
alert(‘Try again.’);
}

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@jazzyjadeauthorJun 24.2003 — Thanks! There's a full chapter about this stuff in my Javascript Bible--I just never knew what it was for. Now I know!
Copy linkTweet thisAlerts:
@jazzyjadeauthorJun 24.2003 — Okay--I read the chapter and tried it out. I think I'm close but it's not quite working. No matter if nothing is in the text box or any word or words are in the the text box, "Working" is returned in the alert box.

var text1 = /bsavingsb/gi

var text2 = /bcheckingb/gi

var text3 = /baccountb/gi

var text4 = /baccountsnumberb/gi

var text4 = /bbanksnumberb/gi

var text4 = /btransitsnumberb/gi

var string1 = new RegExp(text1)

var string2 = new RegExp(text2)

var string3 = new RegExp(text3)

var string4 = new RegExp(text4)

var string5 = new RegExp(text5)

var string6 = new RegExp(text6)

var txtRsp;

function findWords(){

txtRsp = form1.textfield.value;

if (txtRsp.search(string1) != -1){

alert('Working')

} else {

alert('Also Working')

}

}
Copy linkTweet thisAlerts:
@VladdyJun 24.2003 — Search is not the best method to use here - you just care if the match is present. Try this

//define two choices:

test1 = /bsavingsb|bcheckingb|baccountb/gi;

test2 = /baccounts*numberb|bbanks*numberb|btransits*numberb/gi //notice I allowed for more than one space between the phrase words.

Then use the the test method:

stringToTest = /* get your form value here */

testResult = (test1.test(stringToTest)?1:0) + (test2.test(stringToTest)?2:0);

switch(testResult)

{ case 0: /*no matching words found*/

break;

case 1: /*match with the first group of words */

break;

case 2: /* match with the second group of words */

break;

case 3: /* match wth both 1st and 2nd group */

break;

}


Here is the page I put together for testing regular expressions: http://www.klproductions.com/CodeBits/RegExpTester.html

Hope it helps.
Copy linkTweet thisAlerts:
@jazzyjadeauthorJun 25.2003 — Thank you! Your fixes worked like a charm. I had been trying to use the test method but I couldn't understand the book and it didn't provide an example to follow. I must also thank you for showing me how to use the switch statement, which was also new to me.

One last question, if you don't mind.

In the first regular expression, is there a way to avoid "account" also matching up in the second regular expression with "account number"?

I thought that by including the b b and the s* that it would only match "account number" with the specific string "account number".

Is there way of accomplishing this?
Copy linkTweet thisAlerts:
@jazzyjadeauthorJun 25.2003 — I got it! I found the right regular expression to avoid account number matching both "account" and "account number".

var test1 = /bsavingsb|bcheckingb|baccount(?!s*number)b/gi

var test2 = /baccountb(?=bs*
numberb)|bbanks*numberb|btransits*numberb/gi
×

Success!

Help @jazzyjade spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...