Forums

This topic is locked

js form validation problems

Posted 10 Feb 2005 13:09:03
1
has voted
10 Feb 2005 13:09:03 shola law posted:
Hello good pple of the forum i created a javascript to validate a form that will check that whether the text boxes are empty or not, the script is thus:
<script>
function sendform(){
if (document.form1.textfield1.value==""
{alert("the firstname cannot be empty";}
if (document.form2.textfield2.value=="";
{alert("the lastname field cannot be empty);}
}
</script>
<body>
Afterwards, using the onClick keyword in a submit button, i instructed the browser to check whether the fields are empty. If the firstname field is empty the alert msg is displayed and the same goes for the last name field, but if one out the lastname and firstname(vice versa) is not empty, after the alert msg is displayed and i click on the "ok" button on the alert box it clears away my previously entered data on a field that isn't empty, pls is there anything code that can i use so that when a particular field isn't empty, after i 've clicked on the "ok" button on the alert box it doesn't clear off a field that isn't empty?

If u think u can u can

Replies

Replied 10 Feb 2005 21:59:14
10 Feb 2005 21:59:14 Chris Charlton replied:
You may need a... <pre id=code><font face=courier size=2 id=code>return false;</font id=code></pre id=code> to stop the form from processing or doing anything like that. Offhand, that's my idea.

~ ~ ~ ~ ~ ~ ~
Chris Charlton <i>- DMXzone Manager</i>
<font size=1>[ Studio MX/MX2004 | PHP/ASP | SQL | XHTML/CSS | XML | Actionscript | Web Accessibility | MX Extensibility ]</font id=size1>
Replied 11 Feb 2005 18:18:11
11 Feb 2005 18:18:11 shola law replied:
hello thanks for the code, but when i tried validating the form with the return false; declaration thus
&lt;script&gt;
function sendform(){
if (document.form1.textfield1.value==""
{alert("the firstname cannot be empty";}
if (document.form2.textfield2.value=="";
{alert("the lastname field cannot be empty);}
return false;}
&lt;/script&gt;
&lt;body&gt;
Nothing exactly happened, the form was even passed by the browser. Pls, tell me what to do, i really want to get the hang of javascript with issue such as this. If eventually u 've an answer pls include comments to the codes.
Thank u a million

If u think u can u can
Replied 15 Feb 2005 02:12:20
15 Feb 2005 02:12:20 Chris Charlton replied:
Well in most JS (JavaScript) validations I've seen, there's a fallthrough (for when they are all valid) that use a <pre id=code><font face=courier size=2 id=code>return true;</font id=code></pre id=code>
... otherwise they fail by returning an alert and <pre id=code><font face=courier size=2 id=code>return false;</font id=code></pre id=code>

I hope that gives some guidance.

~ ~ ~ ~ ~ ~ ~
Chris Charlton <i>- DMXzone Manager</i>
<font size=1>[ Studio MX/MX2004 | PHP/ASP | SQL | XHTML/CSS | XML | Actionscript | Web Accessibility | MX Extensibility ]</font id=size1>
Replied 15 Feb 2005 11:17:31
15 Feb 2005 11:17:31 Lee Diggins replied:
Hi Shola

Do you have two different forms on the same page, one each for first and last names? If not change the quoted line below to form1, if that's the name of your form.

<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>if (document.form2.textfield2.value=="";
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

This is a nice extension to have:
www.yaromat.com/dw/?ex=Check%20Form%20MX

Sharing Knowledge Saves Valuable Time!!!
~ ~ ~ ~ ~ ~
Lee Diggins - <i>DMXzone Manager</i>
<font size="1">[ Studio MX/MX2004 | ASP -> VBScript/PerlScript/JavaScript | SQL | CSS ]</font>

Reply to this topic