chelvis
09-17-2002, 11:08 PM
I have a form with 3 text boxes. I wrote a validation to make sure the user enters some thing in text box. So according to my javascript, if the user doesnt enter any thing and then if he press the submit button, javascript displays an alert. But the problem is if the user leaves the first text box empty and types some thing in the second or third box, it still displays that alert, which it shouldn't. I tried to re write my javascript but getting errors (probably with the "(" signs and etc). How can I not display an alert if the user just type some thing in the second or third box?
<script language="javascript">
function searchValidation() {
var searchword1=document.quicksearch.searchWord1.value;
if((searchword1=="") || (searchword1==null)) {
window.alert("Enter at least one term to search in the database.");
return false;
}
if(!(searchword1=="")) {
if(searchword1.substring(0,1) == '*') {
window.alert("Search word cannot begin with * character.");
return (false);
}
var searchLength= searchword1.length;
var tempword = searchword1;
var tempLength=0;
while (tempword.substring(0,1) == ' ') {
tempword = tempword.substring(1);
tempLength = tempLength + 1;
}
if ( searchLength == tempLength) {
window.alert("Enter at least one term to search in the database.");
return (false);
}
}
</script>
<FORM name="quicksearch" action="#" METHOD="POST" onSubmit="return searchValidation()" >
<input type="text" name="searchWord1" size="29">
<br>
<input type="text" name="searchWord2" size="29">
<br>
<input type="text" name="searchWord3" size="29">
<input type="image" name="image" src="images/search.gif">
</FORM>
<script language="javascript">
function searchValidation() {
var searchword1=document.quicksearch.searchWord1.value;
if((searchword1=="") || (searchword1==null)) {
window.alert("Enter at least one term to search in the database.");
return false;
}
if(!(searchword1=="")) {
if(searchword1.substring(0,1) == '*') {
window.alert("Search word cannot begin with * character.");
return (false);
}
var searchLength= searchword1.length;
var tempword = searchword1;
var tempLength=0;
while (tempword.substring(0,1) == ' ') {
tempword = tempword.substring(1);
tempLength = tempLength + 1;
}
if ( searchLength == tempLength) {
window.alert("Enter at least one term to search in the database.");
return (false);
}
}
</script>
<FORM name="quicksearch" action="#" METHOD="POST" onSubmit="return searchValidation()" >
<input type="text" name="searchWord1" size="29">
<br>
<input type="text" name="searchWord2" size="29">
<br>
<input type="text" name="searchWord3" size="29">
<input type="image" name="image" src="images/search.gif">
</FORM>