Link to home
Start Free TrialLog in
Avatar of clickclickbang
clickclickbang

asked on

Validating User Input With JavaScript (DateTime, Int, Float)

Experts, I am looking for an example or some links to help me in validating user input via JavaScript. (Yes I know that the best validation is server side, but using JavaScript creates a faster user experience and is required for my needs).

I need to check for int types, float types, and the harder DateTime types. I'm putting these into a MS SQL database so the DateTime formats accepted are numerous. The standard is M/dd/yyyy hh:mm tt, however, I would like something that would allow for a few variations.

I found the following RegEx datetime validation pattern but it is far over my head in regards to undestanding the RegEx and I DO NOT know how to implement a simple RegEx pattern match in JavaScript.

var RegEx = "^((((([13578])|(1[0-2]))[\-\/\s]?(([1-9])|([1-2][0-9])|(3[01])))|((([469])|(11))[\-\/\s]?(([1-9])|([1-2][0-9])|(30)))|(2[\-\/\s]?(([1-9])|([1-2][0-9]))))[\-\/\s]?\d{4})(\s((([1-9])|(1[02]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$"

Thank you for your time!

~ C
ASKER CERTIFIED SOLUTION
Avatar of cezarF
cezarF
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of clickclickbang
clickclickbang

ASKER

For some reason it doesn't seem to be matching for me on this value:

7/13/2007 3:48:41 PM

Any idea why?

I'm using

var RegEx = "^((((([13578])|(1[0-2]))[\-\/\s]?(([1-9])|([1-2][0-9])|(3[01])))|((([469])|(11))[\-\/\s]?(([1-9])|([1-2][0-9])|(30)))|(2[\-\/\s]?(([1-9])|([1-2][0-9]))))[\-\/\s]?\d{4})(\s((([1-9])|(1[02]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$";
                    if (o.value.match(RegEx))
                    {
                        // Pattern match
                    }
                    else
                    {
                        alert("No Match!");
                    }
o is my form field and it is passing the value!

Any ideas what I'm missing? I've tried several patterns from regexlib.com!

Thanks for your input!

~ C
@clickclickbang: the regEx should be enclosed with / and nor with "
ooops. sorry for the typo error

@clickclickbang: the regEx should be enclosed with / and NOT with "
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help!