Form Acceptance

I have a form, a couple of the fields NEED to be filled with something.

I used REQUIRED on the form, and pressed the submit button to go to the next page, and it let me. Bypassing any "required" fields.

I need to be able to disable the "SUBMIT" button I think, until my requirements are met for the other 2 fields. I need these 2 fields not to be blank because one is a Unique index, and one is a main picture that is an ANCHOR for another page.

Then again is this even the right thought process?
Who is Participating?
Mark BradyConnect With a Mentor Principal Data EngineerCommented:
I recommend doing both. Use javascript on the front end to check the required fields. Javascript validation makes the whole form sending experience faster if there is a mistake. The form does not have to be sent via "submit" or an ajax call to the server and the server doesn't have to process the data and send back an error. It is convenience only.

Of course on the back end you absolutely should validate ALL data coming in especially if you are storing and retrieving from a database.

On the javascript (client side) just check the values of each input. ie: var x = document.getElementById('username').value;

if (x == '') {
    alert('Please fill out your username');

Go through any fields that must be filled out the same way always showing an error message of some sort then returning out of the function so the form is not sent.

Rather than use the traditional form submit you should consider using ajax to post your form data. It will avoid two page loads. When you 'submit' a form the browser loads the target script which processes the data then redirects the browser back to the website.

Ajax will send your data to the script (no page redirection) the script processes it and sends back a message to ajax. If there is an error sent back (missing information or something) the whole page does not need to reload. You just show the user what they missed. Very simple and fast.
Ray PaseurCommented:
Please post the form, using the code snippet, thanks.

You may want to use jQuery for a good client-side experience.  Whether or not you choose that, you must validate the input on the server-side.
>>Then again is this even the right thought process?
No.  You are relying on client-side validation.  I can create a perl script and emit a "submit" of your form without your required fields.

Thus, what you really need to do is validate the submitted data on the server and refuse to proceed until your requirements are met.  Here's a page that shows the principle in action:
Ray PaseurCommented:
+1 for what Elvin66 writes here.  These might be useful links.  (Several useless links were deliberately omitted!)
StarThunderAuthor Commented:
Sorry for being so late in this... I had a health problem.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.