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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help validating a form

Status
Not open for further replies.

davidmcolaco

Technical User
Aug 1, 2005
102
PT
Hi,
I'm learning javascript now and I have form made, but now I would like to check and validate if the user has inserted the e-mail address and I searched and made a code for that, but doesn't seem to work. Can someone help me understand why it doesn't work.
The function is this:
function ValidateForm(frm_recuperacao)
{
if(IsEmpty(frm_recuperacao.txtemail))
{
alert(You didn't insert E-mail address!')
frm_recuperacao.txtemail.focus();
return false;
}
return true;
}

And then on the form part I have this code:
<form action="enviar_email_vobis.asp" method="post" name="frm_recuperacao" id="frm_recuperacao" onsubmit="javascript:return ValidateForm(frm_recuperacao)">

Thanks in advance.
 
Make these changes.
Code:
 onsubmit="return ValidateForm(frm_recuperacao)"

Why? The onsubmit attribute of the <FORM> tag is used to define an event handler. The event to be handled is clicking a submit button in the form. An event handler is a script, usually a function, but in this case it is a single line of Javascript code.
Code:
return ValidateForm(frm_recuperacao);

javascript:blahblahblah is used in links to run scripts. In that context the symbol javascript is like http or ftp, and is sometimes referred to as a pseudo-protocol. It is used where an internet protocol would be used, but it is not really one.

But more is needed here. frm_recuperacao is a variable. Have you given it a value? No. In your code it is undefined. So naturally the subroutine can make nothing of it.

It is obvious to us that frm_recuperacao should identify the form, but alas, not obvious to Javascript. A correct way to reference the form is
Code:
return ValidateForm(document.frm_recuperacao);
(Actually Internet Explorer may treat frm_recuperancao as the name of the form because sometimes IE compensates for common mistakes. You may wish to experiment with it.)

Another way to reference the form in this context is
Code:
return ValidateForm(this);
or
Code:
 onsubmit="return ValidateForm(this)"

Then in the function, use a different name for the parameter value to remind yourself that this too is yet another variable, not the specific name of a particular form
Code:
function ValidateForm(vForm)
{
   if(IsEmpty(vForm.txtemail)) 
   { 
      alert(You didn't insert E-mail address!') 
      vForm.txtemail.focus(); 
      return false; 
   }
     
   return true;
}

Aint coding fun!


 
I am having some problems with my validation code as well. Here we go:


Code:
<form method=get action=display.jsp onsubmit="return form_validate(this)">  [COLOR=red]Called the function [/color]

[COLOR=blue]This one works.[/color]

<script>
function form_validate(thisform)
{
alert("you found me!");
return false;
};
</script>

[COLOR=blue]This one does not.[/color]

<script>
function form_validate(myform)
{
   if(IsEmpty(myform.ELEMENT)) 
   { 
      alert("Do something."); 
      myform.ELEMENT.focus(); 
      return false; 
   }
   return true;
}
</script>

ELEMENT is an <input type="text" name="ELEMENT"> element in my form page.

Thanks in advance.

- Tim
 
IsEmpty() is a VBScript function, I believe.

And myform.ELEMENT refers to an object, the form element named ELEMENT, hence it will never be empty, in any sense. Though that is not a meaningful statement in Javascript. myform.ELEMENT could be undefined if you did not have a form element named ELEMENT.

Instead -
Code:
if ( myform.ELEMENT.value == "" ) {}
 
Ah, ok. That has to be the most simplistic one I have seen yet. I have found various ways to do these validation checks, but that is the only one which has worked thus far. Thanks for your assistance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top