Hi there,
Pretty novice to JS, just wondered if anyone could give me a hand please.
I have a form that is passed to itself. I was just wondering if there was a way to get this to validate before it passed to itself.
I was assuming that as the form doesn't properly submit, I could not use onsubmit="return sValidation()", so I have it on the buttons onclick.
Correct assumption or not? Is there a way round this at all and to get it to validate?
<form action="" method="post" name="searchform" >
<table width="200" border="0" cellpadding="2" cellspacing="0" class="adforms">
<tr>
<th width="40" align="left">Name: </th>
<td width="152" align="left"><input type = "text" name = "sname" id ="sename" size="15"/></td>
</tr>
<tr>
<td></td>
<td align="right"><input type="submit" value="Search" onclick="return sValidation(this)"/></td>
</tr>
</table>
</form>
function sValidation(){
var sname = document.getElementById('sename');
var div = document.getElementById('errormsg');
var lets = /^[a-zA-Z\s\-\']+$/;
if((sname.value == '') || (sname.value == ' ')){
div.innerHTML = "<b>Please type in a search term</b>";
sname.focus();
return false;}
else if(sname.value.match(lets)){
return true;}
else{
div.innerHTML = "<b>Only letters please</b>";
sname.focus();
return false;}
}
None of the div.innerHTML = "<b>Only letters please</b>"; are shown at all, its liek it doesn't even see the JS
Thanks in advance