JavaScript form validation for ensuring a date of birth is 4 digits long

Richard Hamilton

I'm trying to ensure a valid date of birth by making sure it's 4 characters long and every character is a digit.

function validateBirth() {
    var year = document.forms["myForm"]["birth_year"].value;
    if (year.match(/\A\d{4}\z/) !== null) {
      alert("Date of birth must be 4 digits");
      return false;
   }
}

I'm trying to use JavaScript's string#match function to search for a regular expression in a value. Here we know that it must contain 4 digits, but that it also must not contain any non-digits.

For example, a string like a4b456 would be invalid. That's why I used the regex tags \A and \z for starting and ending a string respectively.

When I test out this function in jsconsole, it looks like this

var year = "1958"
year.match(/\A\d{4}\z/)
=> null

I tested the exact same regex in rubular and learned that there was a match. Am I getting crazy, or is there something here I'm missing?

I know I can do this using HTML Input Validation, but that won't work on non-modern browsers so I need to use the JavaScript method.

Trott

Start and end of a string for a JavaScript regex are ^ and $ respectively. (\A and \z are valid for start and end in Ruby, but not JavaScript.) So this regex will work:

year.match(/^\d{4}$/);

If you're interested in a Boolean result rather than receiving the actual match, you can do this instead:

/^\d{4}$/.test(year)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

DatePicker validation for Date of Birth

From Dev

Date of Birth validation keeps showing

From Dev

Validation for Birth Date, Joining Date and Leaving Date

From Dev

Javascript form validation taking a long time to run

From Dev

Date of birth validation by using regular expression

From Dev

Basic age validation from date of birth in PHP

From Dev

Date Validation for Birth Date, Joining Date and Leaving Date

From Dev

based on date of birth age not displaying using javascript

From Dev

Date of birth JavaScript function stopped working

From Dev

Stuck at comparing date of birth in the form yyyy/mm/dd

From Dev

Implode separate date of birth values from php form

From Dev

Stuck at comparing date of birth in the form yyyy/mm/dd

From Dev

CakePHP Registration Form: Date of Birth Being returned as array

From Dev

Javascript to calculate a fictional date of birth from todays date and fixed age

From Dev

how to generate a random long with 4 digits in java?

From Dev

Form Validation Angular 4

From Dev

Laravel4 migration column type of Date of Birth

From Dev

Change file "Birth date" for ext4 files?

From Dev

JavaScript validation with alternative numeric and alphabetic digits

From Dev

Javascript password validation at least 2 number digits

From Dev

validation contact form with javascript

From Dev

Contact form validation javascript

From Dev

Form Validation Javascript

From Dev

Javascript validation in form

From Dev

JavaScript Form, with validation

From Dev

JavaScript Form Validation -

From Dev

html form validation with javascript

From Dev

Javascript warning validation form

From Dev

JavaScript Form, with validation

Related Related

HotTag

Archive