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!

OnBlur event and onSubmit Error Checking, how?

Status
Not open for further replies.

WTHolmes

Technical User
Mar 22, 2007
18
0
0
US
Hi Guys,

I am trying to create my first web form. I have the code below, but am stuck trying to make sure that all fields have data using onSubmit. Is it better to validate fields on submission or once they are keyed in?

I am also having trouble with the onBlur command. I have created a function that does work for my number field, however, I would like the check to take place using onBlur.

Help Please! I have spent days working on this and I keep getting more confused and more errors the more "tweaking" I do.

The following code is error free, unfortunately, it doesn't do what I want it to:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<html>
<head>
<script type="text/javascript">
function validate_required(field,alerttxt)
{
with (field)
{
if (value <= 12 || value >= 27)
{alert(alerttxt);return false}
else {return true}
}
}function validate_form(thisform)
{
with (thisform)
{
if (validate_required(number,"Please enter a number BETWEEN 12 and 27")==false)
{number.focus();return false}
}
}
</script>
</head><body>
<form action="submitpage.htm"
onsubmit="return validate_form(this)"
method="post">
<div id="wmbanner">Karma Form </div>
<h1>May your Zen &ndash;&nbsp;Zig&nbsp;Always&nbsp; </h1>
<ul>
</ul>
<p>Please Complete the form below. Refer to the instructions below if necessary. </p>
<ol>
<li>The number field is your <strong>Karma number</strong> and the color field is your <strong>Karma color</strong>. </li>
<li>Press the <strong>Reset button</strong> to clear the form. Press the <strong>Submit button</strong> to submit the form. </li>
</ol>

<HR> <!-- ====================================== -->
<p>
<label>First Name:
<input name="fname" type="text" id="fname" size="27" />
</label>
</p>
<p>
<label>Last Name:
<input name="lname" type="text" id="lname" size="27" />
</label>
</p>
<p>Number:
<input type="text" name="number" size="30">
between 12 and 27
</p>
<p>
<label>Color:
<input name="color" type="text" id="color" size="32" />
</label>
</p>
<p>
<label>
<input name="Reset" type="reset" id="Reset" value="Reset" />
</label>
<input name="submit" type="submit" value="Submit" />
</p>
<p>&nbsp;</p>
</form>
<HR> <!-- ====================================== -->
</body></html>
<body>
</body>
</html>

As you can see, it is pretty simple, just 4 fields and 2 buttons. The Submit button doesn't work as I would like it too, I would like it to tell me when a field is blank, it doesn't have to know which field but that would be cool.

The number field should automatically check to make sure that the number entered is correct once it looses focus. I can't get that to work either.

With your help on those two things, my "Karma Catalog" will be in its highest Chakra and I will have attained zenlike happiness. :)

Thank You!
 
Hi

WTHolmes said:
The following code is error free
No kidding. Your document has 2 [tt]head[/tt] sections. How it could be error free ?
HMTL Validator said:
Failed validation, 8 errors
WTHolmes said:
The number field should automatically check to make sure that the number entered is correct once it looses focus.
And what if it never receives focus, so will neither loose it ? Then no validation will be performed ? Better leave it as it is now, in the [tt]onsubmit[/tt] event handler.

Feherke.
 
Feherke,

Thanks for your response, but I don't understand the 2 head sections. Can you elaborate and tell me what I am doing wrong?

Thanks,

W. Holmes
 
try
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>
            Untitled Document
        </title>
    </head>
    <body>
        <script type="text/javascript">
//<![CDATA[
function validate_required(field,alerttxt)
{
with (field)
{
if (value <= 12 || value >= 27)
  {alert(alerttxt);return false}
else {return true}
}
}function validate_form(thisform)
{
with (thisform)
{
if (validate_required(number,"Please enter a number BETWEEN 12 and 27")==false)
  {number.focus();return false}
}
}
//]]>
</script>
        <form action="submitpage.htm" onsubmit="return validate_form(this)" method="post">

            <div id="wmbanner">
                Karma Form
            </div>
            <h1>
                May your Zen –&nbsp;Zig&nbsp;Always&nbsp;
            </h1>
            <p>
                Please Complete the form below. Refer to the instructions below if necessary.
            </p>

            <ol>
                <li>The number field is your <strong>Karma number</strong> and the color field is your <strong>Karma color</strong>.
                </li>
                <li>Press the <strong>Reset button</strong> to clear the form. Press the <strong>Submit button</strong> to submit the form.
                </li>

            </ol>
            <hr />
            <!-- ====================================== -->
            <p>
                <label>First Name: <input name="fname" type="text" id="fname" size="27" /></label>
            </p>
            <p>
                <label>Last Name: <input name="lname" type="text" id="lname" size="27" /></label>

            </p>
            <p>
                Number: <input type="text" name="number" size="30" /> between 12 and 27
            </p>
            <p>
                <label>Color: <input name="color" type="text" id="color" size="32" /></label>
            </p>
            <p>

                <label><input name="Reset" type="reset" id="Reset" value="Reset" /></label> <input name="submit" type="submit" value="Submit" />
            </p>
            <p>
                &nbsp;
            </p>
        </form>
        <hr />
        <!-- ====================================== -->

    </body>
</html>
 
Hi Mark,

Unless I am doing something wrong, I can't get the code to work properly. It's supposed to check that all fields have data. It's also supposed to check the karma number when it losses focus (onBlur).

Thanks,

W. Holmes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top