Input Form Inaccuracies

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Input Form Inaccuracies

Post by mohson »

Can some one please suggest a good website to help me put in some preventative coding that will help me validate the data in my forms to prevent inaccuracies Ive looked arounf and find small code snippets in java script (ive been told not to use this is it wont work with certain browsers) but nothing major - any suggestions.

I suppose I could just use dreamweavers restrriction coding??

anyway if anyone can suggest a tutorial or give any advice let me know.

Apollogies if this is too much of a basic question
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

If only users would understand how to fill in forms correctly.

When i am constructing forms, i design them in such a way to prevent user input errors from the outset.

One of the most common input errors is with dates as these can be entered in numerous different formats...so i usually present the user with 3 drop down menus...day, month, year...then you know exactly what you are expecting to get from the user.

If this isn't possible, next thing to do is check the user data when the form has been submitted....use REGEX or whatever you need....if error, display the form again and display error message else submit form.

Mark
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

Thanks Mark,

I currently display my dates in UK format dd/mm/yy USING DATE_FORMAT BUT I have been told that I MUST insert them in standard mysql format which yy/dd/mm is there anyway aound this that you know of??
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I, for one, personally can't stand forms that dont' do client side validation. I hate having to submit my data only to find out that I missed something.

why don't you do both (JS and PHP) form validation. that way if the user doesn't have JS working on their specfific browser, you are still covering yourself by checking on the server side.

what kind of fields do you need to validate? If you can let me know a few, I can help you with the JS.

as for inserting dates to the database, you can reformat the dates very easily and then insert them using php.

Code: Select all

date("Y-m-d", strototime($_POSTї'mydatefield']))
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

I like doing client side validation, it saves a lot of wasted time for the user if it is done properly. Here are some liks that may help you.

http://www.peterbailey.net/fValidate/

http://developer.apple.com/internet/web ... ation.html

http://wsabstract.com/script/cutindex13.shtml

http://members.aol.com/perflunk/
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

this was sent to me in a PM...moving it back here to keep it in the thread.
mohson wrote:Thanks for your reply - the only validation im doing so far is 'you have not entered certain information please provide it'

this obviously isnt enough - would you have any advice on validating numbers to make sure user enters a number

and validating email would be helpful?

Below is what I have done so far - if you do have some idea on how to do the two mentioned above then it would be helpful to show me how to incoporate it into the code - as when I try and incorporate other js with what I already have I find that it gets confusing and doesnt work.

Code: Select all

<SCRIPT LANGUAGE="javascript">

<!-- 

function focus()
&#123;
  document.forms&#1111;0].FirstName.focus();
&#125;

function checkme() //check for required fields
&#123;
    if (document.forms&#1111;0].firstname.value == "")
    &#123;alert("You did not enter a first name. Please provide it.");
    document.forms&#1111;0].firstname.focus();return(false)
    &#125;

    if (document.forms&#1111;0].surname.value == "")
    &#123;alert("You did not enter a Surname. Please provide it.");
    document.forms&#1111;0].surname.focus();return(false)
    &#125;

    if (document.forms&#1111;0].organisation.value == "")
    &#123;alert("You did not enter an Organisation. Please provide it.");
    document.forms&#1111;0].organisation.focus();return(false)
    &#125;
    
    if (document.forms&#1111;0].org_id.value == "")
    &#123;alert("You did not enter an Organisation ID. Please provide it.");
    document.forms&#1111;0].organisation.focus();return(false)
    &#125;
&#125;



//-->


//-->

</SCRIPT>
</head>
<body onLoad="focus()"> 

<form method="post" action="processpeople.html" onSubmit="return checkme()" 
name=Feedback> 


           and then below are all my text boxes
For starters, making it return false after one criteria is not met, is not good practice. If they have two things wrong, they won't know it until they try to submit the form twice.

here's a better way to do that:

untested:

Code: Select all

<script>
var msg = "The following information needs attention \n";
with(document.formName)&#123;
if(field1.value == "")&#123;
msg += "Field1 can not be left blank \n";
&#125;
if(isNaN(parseInt(field2.value)))&#123;
msg += "Field 2 must be a number \n";
&#125;
if(msg.length > (whatever the length of the first one is))&#123;
alert(msg);
return false;

&#125;else&#123;
return true;
&#125;
&#125;
the above will return one alert with all the missing stuff.

field 2 up there is how you check for numbers. To check for valid email address just search the string for a "@" and a "." and maybe make sure there's a domain extension

in the future, it would be wise to keep this stuff in the threads so that others can see the resolutions.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

In my opinion validation on the user side is a waste of coding time. You have to validate on the PHP side in case the user has javascript switched off (on purpose perhaps, in order to enter invalid data..) .. so why bother writing two sets of code to do the same thing?

Also, if you validate in JavaScript you're probably giving the user an alert box with their errors in.. that vanishes once they click ok. I prefer to change the form and highlight areas they went wrong*.. Thats considerably more difficult in Javascript.

* Example: http://www.ooer.com/index.php?section=email .. try submitting an empty message.
Post Reply