By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,130 Members | 2,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,130 IT Pros & Developers. It's quick & easy.

Validating form fields aren't empty

P: n/a
Hello Everyone,

I am having some trouble getting my JavaScript to validate my form
fields. I've included more information below. My explaination of the
code is in ALL CAPS so it stands out from the code. Can someone tell
me what I am doing wrong?

Thanks!
Anthony
THIS JAVASCRIPT IS IN THE <headSECTION OF MY HTML PAGE. IT IS
SUPPOSED TO VALIDATE THAT THE FORM FIELDS NAMED "uname" AND "pswd" ARE
NOT EMPTY. HOWEVER IT IS NEVER TRIGGERED. PLEASE SEE THE HTML CODE
BELOW THE SCRIPT TO SEE HOW I AM TRYING TO INVOKE IT.

<Script Language="JavaScript">
<!--
function validate(frm){
if(frm.uname.value.length == 0 || frm.pswd.value.length == 0){
alert("You must enter both a username and password to access this
service!");
frm.uname.focus();
return false;
}
-->
</Script>

HERE IS THE FORM DECLARACTION THAT SHOULD TRIGGER IT:

<form name="MyForm" action="signin.php" method="post" onSubmit="return
validate(MyForm)">

I'VE ALSO TRIED THE FORM DECLARATION WITH A ; AT THE END OF THE RETURN
CALL AND IT DIDN'T WORK. ADDITIONALLY, I'VE ATTEMPTED TO INVOKE IT IN
THE SUBMIT BUTTONS onClick EVENT AS:

<input type="submit" value="Sign In" onClick="return
validate(myForm)" />

AND IT DIDN'T WORK EITHER. THE FORM IS ALWAYS SUBMITTED.

Apr 15 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Anthony P wrote:
Hello Everyone,

I am having some trouble getting my JavaScript to validate my form
fields. I've included more information below. My explaination of the
code is in ALL CAPS so it stands out from the code. Can someone tell
me what I am doing wrong?

Thanks!
Anthony
THIS JAVASCRIPT IS IN THE <headSECTION OF MY HTML PAGE. IT IS
SUPPOSED TO VALIDATE THAT THE FORM FIELDS NAMED "uname" AND "pswd" ARE
NOT EMPTY. HOWEVER IT IS NEVER TRIGGERED. PLEASE SEE THE HTML CODE
BELOW THE SCRIPT TO SEE HOW I AM TRYING TO INVOKE IT.

<Script Language="JavaScript">
<!--
function validate(frm){
if(frm.uname.value.length == 0 || frm.pswd.value.length == 0){
alert("You must enter both a username and password to access this
service!");
frm.uname.focus();
return false;
}
-->
</Script>

HERE IS THE FORM DECLARACTION THAT SHOULD TRIGGER IT:

<form name="MyForm" action="signin.php" method="post" onSubmit="return
validate(MyForm)">
onsubmit="return validate(this)"

Mick
>
I'VE ALSO TRIED THE FORM DECLARATION WITH A ; AT THE END OF THE RETURN
CALL AND IT DIDN'T WORK. ADDITIONALLY, I'VE ATTEMPTED TO INVOKE IT IN
THE SUBMIT BUTTONS onClick EVENT AS:

<input type="submit" value="Sign In" onClick="return
validate(myForm)" />

AND IT DIDN'T WORK EITHER. THE FORM IS ALWAYS SUBMITTED.
Apr 16 '07 #2

P: n/a
onsubmit="return validate(this)"
>
Mick
Nope, still submitting without validation. But thank you anyway for
your answer :-)

Anthony

Apr 16 '07 #3

P: n/a
Changed the function code to this and it worked:

function validate(){

if(document.getElementById('uname').value.length == 0 ||
document.getElementById('pswd').value.length == 0){
alert("You must enter both a username and password to access this
service!");
document.getElementById('uname').focus();
return false;
}
}

Thanks for all who helped.

Apr 16 '07 #4

P: n/a
Anthony P said the following on 4/15/2007 6:56 PM:
Hello Everyone,
My name isn't "Everyone" but Hello to you as well.

<snip>
<form name="MyForm" action="signin.php" method="post" onSubmit="return
validate(MyForm)">
That should give an error as MyForm - in that scenario - is trying to
reference a variable that doesn't exist.

validate(this)

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 16 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.