Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 477
  • Last Modified:

Check that field has minimum two words

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
Dinkleburger
Asked:
Dinkleburger
  • 6
  • 4
  • 2
  • +1
1 Solution
 
fritz_the_blankCommented:
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
 
fritz_the_blankCommented:
<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
 
NetGrooveCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Michel PlungjanIT ExpertCommented:
Fritz, you must mean

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



0
 
NetGrooveCommented:
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
 
fritz_the_blankCommented:
@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
 
apparitionCommented:
<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
 
NetGrooveCommented:
Small note: single balnk will disturb the word checking in your search for blank.
0
 
apparitionCommented:
my sugestion makes sue the user cannot fool the script my entering a space in front of the first word
0
 
NetGrooveCommented:
My proposal does it also, but with a single line :)
0
 
NetGrooveCommented:
Thanks for the points.
0
 
Michel PlungjanIT ExpertCommented:
Fritz: " here" wil also trigger your code
0
 
fritz_the_blankCommented:
@Michel:

I don't understand, "here" ?

FtB
0
 
NetGrooveCommented:
You missed the leading blank.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now