Jump to content

Basic Form Help on script and a question related to JS Validation RESOLVED


kenwvs

Recommended Posts

Hi All

I have never used a form validation (except for FP) and am trying to stay away from FP and learn how to do things.

I have a form that has 16 fields to fill in.  Some are required, and some are not.  At the moment, to try my script out, I have only made the User ID a required field.  My understanding is if this field is left blank, it would result in a pop up saying "Please enter a valid User Name" (this name could be anything in this case).

For some reason, if I leave the field blank, it proceeds as if nothing is wrong.  I have borrowed this script from a help site, and have changed it for my names, etc.  Was I correct in reading that the script should be placed in the <HEAD> portion of the page and not in the <BODY>? 

Here is the script I am using.  When I get this field to work properly, I will add the additional lines for each of the other required fields.  When you press submit it takes you to a confirmation page, which is PHP and allows you to make a choice as to what you want to do next and where you want to go.

[quote]<script language="JavaScript" type="text/javascript">
<!--
function checkform ( form )
{
if (form.id.value == "") {
    alert( "Please enter a valid User ID." );
    form.id.focus();
    return false ;
  }
  return true ;
}
//-->
</script>[/quote]
and then I have this added in the <body> at the start of the form.

[quote]<form name="form" action="upload2.php" method="POST" onsubmit="return checkForm()">[/quote]
The button is configured to be a submit button

[quote]<input type="submit" name="Submit" value="Add Item" />[/quote]

On this line (form.id.value == "")  am I correct in reading this that it basically says it the input field is blank (""), nothing in here, then return it false?  If I wanted it to be anything but the number 123, could I put 123 in there and it would return false, only if 123 was entered?

Thanks for the help

Ken
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.