Solved

Check that field has minimum two words

Posted on 2003-11-28
14
456 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

789 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