Solved

Check that field has minimum two words

Posted on 2003-11-28
14
445 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now