Solved

Check that field has minimum two words

Posted on 2003-11-28
14
464 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

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. …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

726 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