Dinkleburger
asked on
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
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
<HEAD>
<TITLE>Teledex </TITLE>
<script language="javascript">
function atLeastTwo(){
strWords = document.forms[0].elements ['keyword' ].value.sp lit(" ")
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">
<TITLE>Teledex </TITLE>
<script language="javascript">
function atLeastTwo(){
strWords = document.forms[0].elements
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">
Here my proposal:
<html><HEAD><TITLE>Teledex </TITLE>
<script>
function check(theForm){
if(theForm.keyword.value.m atch(/\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>
<html><HEAD><TITLE>Teledex
<script>
function check(theForm){
if(theForm.keyword.value.m
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>
Fritz, you must mean
strWords = document.forms[0].elements ['keyword' ].value.sp lit(" ")
if(strWords.length<2){
strWords = document.forms[0].elements
if(strWords.length<2){
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@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">
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
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">
<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(retValu e.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(retValu e.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(retValu e.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.f orm)" value="Search">
<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(retValu
while (ch == " ") { // Check for spaces at the end of the string
retValue = retValue.substring(0, retValue.length-1);
ch = retValue.substring(retValu
}
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(retValu
}
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.f
Small note: single balnk will disturb the word checking in your search for blank.
my sugestion makes sue the user cannot fool the script my entering a space in front of the first word
My proposal does it also, but with a single line :)
Thanks for the points.
Fritz: " here" wil also trigger your code
@Michel:
I don't understand, "here" ?
FtB
I don't understand, "here" ?
FtB
You missed the leading blank.
strWords = document.forms[0].elements
if(strWords.indexOf(" ")<0){
alert("You must enter at least two words");
return false;
}
return true;