form validation

a place to get help

Moderator: 1.2 Moderators

Forum rules
  • Every time you need help create a new thread (but please use the search function before, as your problem maybe has already been solved)
  • use code highlighting (help)
  • After your problem has been solved you have to rename the subject to "[solved] <Topicname>" AND set the Topic icon as solved (the green check)

form validation

Postby basatuwinggabi on Fri Jun 04, 2010 7:25 pm

Hi again.
I have a form where in it will validate required fields. I got the code working to validate only required fields.
My problem is on how to validate fields where in there is a "from" and "to". Example of this would be "Year From" and "Year To".

Conditions:
- If "From" is supplied, "To" should not be empty.
- If "To" is supplied, "From" should not be empty.
- "From" cannot be greater than or equal to "To".
- Should only be numeric.

If possible, also on dates. (yyyy-mm-dd)
- Check if format is (yyyy-mm-dd)

Thank you.
basatuwinggabi
mootools connoisseurs
 
Posts: 38
Joined: Tue May 18, 2010 5:18 pm

Re: form validation

Postby fyrye on Sat Jun 05, 2010 12:03 am

javascript regular expressions can accomplish this for you

http://www.w3schools.com/jsref/jsref_obj_regexp.asp

Simply test your pattern vs the input, if a match is found than it's valid otherwise it's invalid.

There are several examples of validating date formats in google.
/^(\d{4})[./-](\d{1,2})[./-](\d{1,2})$/
For one
which should accept yyyy.mm.dd or yyyy-mm-dd or yyyy/mm/dd
How it reads is:
Beginning of line must be digits of 4 characters, + (. or / or -) + digits of 1 or 2 + (. or / or -) + digits of 1 or 2


In my personal opinion, I wouldn't use regex to validate a specific format of date, but rather convert the inputed date to the desired format.
Uniforming if you will.
You can use mootools.more.date to accomplish this
http://mootools.net/docs/more/Native/Date#Date:parse
and
http://mootools.net/docs/more/Native/Date#Date:format
First to parse the inputed data to a valid date, then format to format it to your desired format.
You can use the DOM to show the results to the user.
Then you can use
http://mootools.net/docs/more/Native/Da ... e:timeDiff
To verify that Time A is less than Time B

Alternatively you can look up javascript functions for datediff, but won't be as flexible as MooTools.

Do you have a fall back in-case your users disabled javascript?
EG: HA form validation, (disable javascript), Submit form by-passing validation, and potentially crashing form processor, exposing database
User avatar
fyrye
mootools enthusiast
 
Posts: 163
Joined: Sat Dec 20, 2008 10:16 am

Re: form validation

Postby basatuwinggabi on Sat Jun 05, 2010 5:16 am

Thanks for the reply fyrye. ;)
I got the validation already in just javascript wherein "onchange", it will validate the values on the textbox and if it does not satisfy the condition, it will display a warning message after that textbox. I just want to convert it to mootools to add a little "effect" on it. (I just love the fading effects of mootools :lol: )

Question:
Why not use regex?
To validate the dates, I split them to the delimiter of (-) then validate the array returned. Is that ok? It seems to be working fine but I don't know if it is acceptable in business logic.

Yes, I do have a fallback when users disable javascript on their browser by adding the same conditions in javascript on the back-end (php). Is it sufficient enough?

Thank you so much!
basatuwinggabi
mootools connoisseurs
 
Posts: 38
Joined: Tue May 18, 2010 5:18 pm

Re: form validation

Postby fyrye on Sun Jun 06, 2010 10:00 am

1) Instead of relying on regex to validate the user's input, and frustrating them when they finish the form to find out their data was invalid and they have to input it a certain way, I would just change it to the format you want

So say I enter 12/28/2008 for the date, your script would automatically format it to 2008-12-28 for me, either onblur or onsubmit (i like the onblur myself) which is IMO better than "Sorry the date you entered is not formatted correctly, please change it" since the date format can be handled by you as long as it's a date.

Now in order to stop an error of an invalid date from javascript/mootools date.parse, I would check the value before trying reformat it. In which case, I would inform the user that it wasn't a valid date. So a user can't just enter, "alkdjalsyuiowye" and javascript throws an error killing the validator.
This way you can supply several means of inputting dates EG: US/UK/ISO/UNIX, etc, as valid date inputs, but format the date to the way you like when a valid date in any format is typed in.

2) Yes the php validation is awesome and should never be overlooked (never trust the user input), but I would follow the same guidelines as above.
Verify the date is a valid format (of any formats), if it's not a date report it back to the user, if it is a date then format it to the way you like and continue on.


Were you needing help with the Fx as well?
User avatar
fyrye
mootools enthusiast
 
Posts: 163
Joined: Sat Dec 20, 2008 10:16 am

[solved] Re: form validation

Postby basatuwinggabi on Tue Jun 08, 2010 4:04 am

Thanks for the wonderful reply fyrye.

I guess I'll just create another post for the Fx.
Thank you again :D
basatuwinggabi
mootools connoisseurs
 
Posts: 38
Joined: Tue May 18, 2010 5:18 pm


Return to Help

Who is online

Users browsing this forum: No registered users and 3 guests