Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form validation 1

Status
Not open for further replies.

kelly5518

Technical User
Aug 1, 2005
72
US
Hi,

Can someone tell me what's wrong with this code that it's not working:
Code:
<script type="text/javascript">
function validateForm(){
stripSpaces(frm)
function stripSpaces(frm) {
    var x = frm.email.value;
    frm.email.value = (x.replace(/^\W+/,'')).replace(/\W+$/,'');
    var z = frm.Message.value;
    frm.Message.value = (z.replace(/^\W+/,'')).replace(/\W+$/,'');
   }
if (document.form1.email.value ==""){
alert ("Please enter your email.");
document.form1.email.focus();
return false;
}
if (document.form1.Message.value == ""){
alert("Please enter your message.");
return false;
}
return true;
}
</script>

Thans a bunch,
 
Not working" covers a lot of territory. What is it not doing? What error message are you getting?
 
Sorry, no error message, it's just going ahead and sending the form through and it should strip spaces then check for empty form fields.
 
Have you assigned a value to a variable named frm somewhere else in your script? If not that is the problem and you could do that like so -
Code:
<html>
...
<form onsubmit="validateForm(this)">
...
<script type="text/javascript">
function validateForm(frm){
  stripSpaces(frm)
...
</script>
...
</html>


Regular expressions are not my strong point so i cannot evaluate those. You might get some more information by adding an alert("before: " + x) and an alert("after:" + frm.email.value) inside the stripSpaces function. This is just for debugging, remove them when you solve the problem.

HTH.
 
That seems to have fixed it. Thanks so much rac2!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top