Solved

Check that field has minimum two words

Posted on 2003-11-28
14
457 Views
Last Modified: 2012-05-04
Hello
the code below is for a search


START CODE
<HEAD><TITLE>Teledex </TITLE></HEAD>
<BODY BGCOLOR="#ffdppp">
<H1>Teledex</H1>

<HR><P>

<H2>Search the Teledex</H2>
Search full name.<br>
<p>
<FORM ACTION="/cgi-bin/nvra.pl" METHOD="POST">
Enter a keyword to search for: <INPUT TYPE="text" NAME="keyword">
<INPUT TYPE="hidden" NAME="act" VALUE="search">
<INPUT TYPE="submit" VALUE="Start Search">

END CODE

Can someone make the variable "keyword" require minimum 2 words to be entered in the text field
and if there is not minimum 2 words then the search cannot start...

example i want the search to start if the text field contains  JOHN SMITH
but not start if the field only contains SMITH
Thanks DB


0
Comment
Question by:Dinkleburger
  • 6
  • 4
  • 2
  • +1
14 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9837860
Try this:

strWords = document.forms[0].elements['keyword'].value.split(" ")
if(strWords.indexOf(" ")<0){
   alert("You must enter at least two words");
   return false;
}
return true;
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9837875
<HEAD>
<TITLE>Teledex </TITLE>
<script language="javascript">
function atLeastTwo(){
strWords = document.forms[0].elements['keyword'].value.split(" ")
if(strWords.indexOf(" ")<0){
   alert("You must enter at least two words");
   return false;
}
return true;


}

</script>
</HEAD>
<BODY BGCOLOR="#ffdppp">
<H1>Teledex</H1>

<HR><P>

<H2>Search the Teledex</H2>
Search full name.<br>
<p>
<FORM ACTION="/cgi-bin/nvra.pl" METHOD="POST" onSubmit="return atLeastTwo()">
Enter a keyword to search for: <INPUT TYPE="text" NAME="keyword">
<INPUT TYPE="hidden" NAME="act" VALUE="search">
<INPUT TYPE="submit" VALUE="Start Search">

0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9837922
Here my proposal:

<html><HEAD><TITLE>Teledex </TITLE>
<script>
function check(theForm){
  if(theForm.keyword.value.match(/\w+\ \w+/)){
    return true;
  } else {
    alert("Enter at least two words.");
    theForm.keyword.focus();
    return false;
  }
}
</script>
</HEAD>
<BODY BGCOLOR="#ffdppp">
<H1>Teledex</H1>
<HR><P>
<H2>Search the Teledex</H2>
Search full name.<br>
<p>
<FORM ACTION="/cgi-bin/nvra.pl" METHOD="POST" onSubmit="return check(this)">
Enter a keyword to search for: <INPUT TYPE="text" NAME="keyword">
<INPUT TYPE="hidden" NAME="act" VALUE="search">
<INPUT TYPE="submit" VALUE="Start Search">
</form>
</body>
</html>


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9837948
Fritz, you must mean

strWords = document.forms[0].elements['keyword'].value.split(" ")
if(strWords.length<2){



0
 
LVL 10

Accepted Solution

by:
NetGroove earned 80 total points
ID: 9837956
Small improvement:

<html><HEAD><TITLE>Teledex </TITLE>
<script>
function check(theForm){
  if(theForm.keyword.value.match(/\w+\ {1,}\w+/)){
    return true;
  } else {
    alert("Enter at least two words.");
    theForm.keyword.focus();
    return false;
  }
}
</script>
</HEAD>
<BODY BGCOLOR="#ffdppp">
<H1>Teledex</H1>
<HR><P>
<H2>Search the Teledex</H2>
Search full name.<br>
<p>
<FORM ACTION="/cgi-bin/nvra.pl" METHOD="POST" onSubmit="return check(this)">
Enter a keyword to search for: <INPUT TYPE="text" NAME="keyword">
<INPUT TYPE="hidden" NAME="act" VALUE="search">
<INPUT TYPE="submit" VALUE="Start Search">
</form>
</body>
</html>



This allows more then one blank as separator, but requires ta least two words with at least one blank in betwean.

0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9837971
@Michele:

A stupid mistake! At first I was going to use the split method, but then changed my mind and decided to use the indexOf() method but forgot to change things. So, with the correction:

<HEAD>
<TITLE>Teledex </TITLE>
<script language="javascript">
function atLeastTwo(){
strWords = document.forms[0].elements['keyword'].value;
if(strWords.indexOf(" ")<0){
   alert("You must enter at least two words");
   return false;
}
return true;


}

</script>
</HEAD>
<BODY BGCOLOR="#ffdppp">
<H1>Teledex</H1>

<HR><P>

<H2>Search the Teledex</H2>
Search full name.<br>
<p>
<FORM ACTION="/cgi-bin/nvra.pl" METHOD="POST" onSubmit="return atLeastTwo()">
Enter a keyword to search for: <INPUT TYPE="text" NAME="keyword">
<INPUT TYPE="hidden" NAME="act" VALUE="search">
<INPUT TYPE="submit" VALUE="Start Search">


0
 
LVL 5

Expert Comment

by:apparition
ID: 9838046
<HEAD>
<TITLE>Teledex </TITLE>
<script language="javascript">
function trim(inputString) {

   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function ErrorCheck(inform){
      SearchText = trim(inform.keyword.value)
      arr=SearchText.split(" ");
      if (arr.length > 1) {
            inform.submit()
      }
      else
            alert ("Please enter atleast two words")
}


</script>
</HEAD>
<BODY BGCOLOR="#ffdppp">
<H1>Teledex</H1>

<HR><P>

<H2>Search the Teledex</H2>
Search full name.<br>
<p>
<FORM ACTION="/cgi-bin/nvra.pl" METHOD="POST">
Enter a keyword to search for: <INPUT TYPE="text" NAME="keyword">
<INPUT TYPE="hidden" NAME="act" VALUE="search">
<INPUT TYPE="button" onclick="ErrorCheck(this.form)" value="Search">
0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9838052
Small note: single balnk will disturb the word checking in your search for blank.
0
 
LVL 5

Expert Comment

by:apparition
ID: 9838058
my sugestion makes sue the user cannot fool the script my entering a space in front of the first word
0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9838116
My proposal does it also, but with a single line :)
0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9838570
Thanks for the points.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9839207
Fritz: " here" wil also trigger your code
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9839258
@Michel:

I don't understand, "here" ?

FtB
0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9839282
You missed the leading blank.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
AJAX ModalPopupExtender will Not Hide in Asp.net 1 30
How to filter by key press ? 6 53
window close link 7 32
Table header must be on top 2 20
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question