[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 503
  • Last Modified:

Birthday Reg Ex MVC3

Hi!

In an MVC3 C# app I need to watch that a birthdate is entered into a field (mandatory) and is also between 20 and 25 years old.  Is it possible to Reg Ex a date range that changes like that to be sure it's always a certain aged date? Or better to do it in a class function check?
Thanks,
Jo
0
BothWorldsJo
Asked:
BothWorldsJo
  • 5
  • 3
  • 2
1 Solution
 
binaryevoCommented:
i would create a validation function and utilize jquery:

var regex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if(regex.test($('#brithday').val())){ //do something } 

Open in new window


Note this is not the correct regex expression I just used it as an example.

You can also do regex testing server side...
0
 
BothWorldsJoAuthor Commented:
Sorry - how does the jquery check that the birthdate is between certain years ago?
0
 
binaryevoCommented:
It doesnt per say, i was just telling the "how" i would do the validation, forgive me if i wasnt clear.  I thought about this a little bit more and honesty using a regex to do that type of validation is probably not the best way to go about it.  I would use a regex to validate that the dates are valid and then compare them to the range.  
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
BothWorldsJoAuthor Commented:
Might you know how the comparison would be written in MVC / razor?
0
 
jmcmunnCommented:
I would do the validation on submit of the form (assuming it is in the form).  Pull it out of the formcollection (or model) in the post method of the controller.  Alternately you could subscribe to the event of the text changing on the actual control and check it in javascript/jquery...but I find it easier to do C#.  :-)

Then just parse the value out as a datetime and compare it to the date range.  If you need examples of this let us know, there are tons of generic validation examples on the web.

some quick psuedo code...in the post action of the controller class
var dateEntered = formcollection["somedatefield"]
if( [dateEntered not in range comparison])
   ModelState.AddModelError("somedatefield", "some error message");
0
 
BothWorldsJoAuthor Commented:
Thanks for the info, could I ask help on the examples?  The ones I found on the net didn't get into the details needed for the date / age comparison but were more if they were greater than some static date, etc.
0
 
jmcmunnCommented:

Here seems to be an example...you may have to translate it or tweak it a bit to fit your needs.

http://developer.force.com/cookbook/recipe/calculating-age-from-date-of-birth
0
 
BothWorldsJoAuthor Commented:
Thanks, and my apologies a bit.  It's not so much how to do the calculation but how to weave it into the MVC 3 view/model so that it's caught and the student is kept on the page since it's not a client side reg ex kind of thing.
0
 
jmcmunnCommented:

Nothing to do in the view really...make sure some control is hooked up to some field and that they are able to enter a date.

In the controller, you need an action method that the form will post to (google it, it's easy)

In this action method, do the above logic for the validation.  By setting an error in the modelstate and then reloading the same view it will essentially keep the user from proceeding and show them a message.  A simple search on Google for MVC validation will show lots of examples of this kind of thing.

If everything is ok...forward them along to a new view instead of setting an error.
0
 
BothWorldsJoAuthor Commented:
Sounds good, thanks for the details.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now