Jump to content

Realtime Form Validation


rastacre

Recommended Posts

Hi all,

 

I'm new to php and webdev in general, so i'm trying many things on my own which usually work but this time i'm completely stuck...

 

Ok i made a script that automatically generates a form to add/upate/delete stuff from a database. the db have various tables, so the script automatically generates the form pulling the relevant fields from the table that the user chooses.

 

I implemented the form validation via php so it simply call the validation via the action attribute of the form. So far so good.

 

Now I wanted to try to implement a realtime validation:

let's say that i have a table called "category" where of course i have cat_id and cat_name

let's say i want to add a new value to such table. The form script generates the form requiring just the name of a new category.

So at this point if i input an existing name and send the form, the validation script will redraw the form and show an error by looking for the name value in the appropriate table.

 

But what I'm aiming to is to also implement a realtime validation that will check the user input and show the error before the form is sent.

 

I would just like to know if this is possible, and what kind of approach shud be taken.

 

I was thinking about pulling the values from the table at form generation, and then use a js function to compare the user input (onkeyup i think) against the values (maybe stored in an array) previously pulled.

In this regard is there a way to pass variable data from a php script to a js script?

 

Thanks in advance

Link to comment
Share on other sites

onkeyup would be a poor choice for performing form validations. it would be too intensive and be difficult to prevent false-positive. Ex: there is an existing field called 'name' and the user tries to create a new field called 'name_suffix'. As soon as the user enteres 'name' it would generate an error and most likely prevent the user from contuniuing. onchange would be a much better choice.

 

Yes you can do this with AJAX, but you also still need to implement the validation server-side once the form is submitted in case the user does not have JS enabled. I would aslo agree that generating an array of the values to be validated for uniqueness instead of sending an individual request each time the user changes the value.

Link to comment
Share on other sites

Everything the last poster said was pretty much spot on. Just to add, you should make the function run onblur. That way it wont provide the false negatives (and positives as well) that he mentioned.

 

Although sending back an array of all the values could get very bulky depending on the amount of data needed to be sent. AJAX is definitely the way to do this. Its a new technology that basically uses javascript to make requests in the background, then reloads parts of a page rather than the whole page. There is an AJAX section on this forum.

Link to comment
Share on other sites

Why use onblur? If a user enters a field and makes no changes, why would you need to revalidate it? Granted, there could be some situations where onblur might be useful, such as if the validation parameters are changing in real-time. But, those situations are few and far between. I still stand by using onchange for form validations.

Link to comment
Share on other sites

I hear what you are saying. I think its 6 of one and 1/2 dozen of the other though. There are times when onblur would be preferable, like if the user left the page for 10 minutes and then came back and was going through and making some changes. Someone may have registered the name in the meantime. Not the most likely thing to happen, but not impossible either. Either of them will work fine though.

 

Link to comment
Share on other sites

OK thanks everyone,

 

I'll see what I can do with the info u all gave me.

 

Just to clarify, I'm going to leave the php validation running at form submit just to prevent users to input incorrect values.

 

I thought that ajax would have been the answer, but without knowing which direction to take I preferred to ask here before spending days looking into something it might not be useful :)

 

Thanks again guys, I'll post my findings and results as soon as I get things to work

Link to comment
Share on other sites

Hi All,

 

just to let you know, I got the validation right.

 

I simply used the AJAX example code from the AJAX section, modified to suit my script, and it worked straight away.

 

This is a very informative forum, I hope one day i'll be able to give advice here as well :)

 

Thanks again

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.