PDA

View Full Version : Form validation not working


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>

umm
09-17-2002, 11:49 PM
This should give you the general idea

<script language="javascript" type="text/javascript">
<!-- ;

function validate() {
var f=document.quicksearch
if(f.searchWord1.value=="" && (f.searchWord2.value=="" || f.searchWord2.value=="")){
alert("thats NOT ok")
f.searchword1.focus();
return false;
}else
if(f.searchWord1.value=="" && (f.searchWord2.value!="" || f.searchWord2.value!="")){
alert("thats ok")
}
return true
}

// -->
</script>

adios
09-18-2002, 05:38 AM
Think this is close; borrowed it from elsewhere:


<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

var chkFlds = new Array('searchWord1','searchWord2','searchWord3');//check these

function searchValidation(f) {
var el, e = input = 0, msg = '', which;
while (el = f[chkFlds[e++]]) if (el.value && !/^\s+$/.test(el.value)) input++;
if (!input) {
alert('Enter at least one term to search in the database.');
f[chkFlds[0]].focus();
f[chkFlds[0]].select();
return false;
}
e = 0;
while (el = f[chkFlds[e++]]) if (/^\s*\*/.test(el.value)) {
msg = 'Search terms cannot begin with an asterisk.';
which = el;
break;
}
if (msg) {
alert(msg);
which.focus();
which.select();
return false;
}
return true;
}

</script>
</head>
<body>
<FORM name="quicksearch" action="javascript:alert('OK')" METHOD="POST"
onsubmit="return searchValidation(this)">
<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>
</body>
</html>