Legless
Legless

Reputation: 53

Javascript Validation Where String Can Be A Date Or A Specific String

I'm looking for a way to validate if a date has been entered in dd/mm/yyyy format OR if a specific string has been entered. The string is TBC

So any date would be valid as long as it was dd/mm/yyy and the string TBC would be valid. Anything else would be invalid.

Thanks in advance

Upvotes: 0

Views: 47

Answers (1)

hamed
hamed

Reputation: 8043

You should use regular expression like this:

var datePattern = /^\d{2}[/]\d{2}[/]\d{4}$/
if(value.match(datePattern) || value == "TBC")
   //ok

value is your input that you want to validate it. Remember you also need to validate content of date, in addition to format. For example 32/01/2015 or 20/13/2015 have correct format, but have not valid content, so they should not be allowed.

Upvotes: 1

Related Questions