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

Recommended Answers

All 4 Replies

Check line 3. You may have a spelling error (sname instead of sename)?

@f_attencia,
no becasue its using the object

document.getElementById('sename');

and the 'ID' of the text box is

id ="sename"

whereas its the NAME that is 'sname'
2 different things.

Thanks for the reply tho

Hey thanks for the reply.
I didn't forget that, its further down the script, I just thought it wasn't worth adding in as its one of the first things i checked :P

Thanks anyway.

EDIT:
Ok got this working finally. Literally stripped the whole thing out and rewrote it, changing things around. Turns out it was

<script language="text/javascript">

It worked when it was either

<script>  OR <script language="javascript">

Just for some flipping reason really didn't like "text" in there as well. Anyone seen this before?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.