regex - date format OR not specified/empty - javascript

I have a regex problem that I need some advice on. I am using a jquery plugin that validates input fields on a JSP. I am using the date validation. It all works fine but this field is not required and is not marked as required. When the user hits submit it shows a warning on the date input field since the empty string does not match the validation of a date. I need some way to say that nothing or a date is valid in a regex.
Here is the regex for the date; is there any way to put "or empty" into this also:
"date":{
"regex":"/^[0-9]{1,2}\-\[0-9]{1,2}\-\[0-9]{4}$/",
"alertText":"* Invalid date, must be in MM-DD-YYYY format"},
Thanks in advance

Try:
/^([0-9]{1,2}\-\[0-9]{1,2}\-\[0-9]{4})?$/
It will then match when a valid date is supplied, or the field is left blank.

(^[0-9]{1,2}-[0-9]{1,2}-[0-9]{4}$|^$)

Related

Javascript - Check if string have a special format

I have a text field and the user should insert there numbers seperated with a comma. For example:
1,2,3,4,50,100
How can I check now if the value of the text field have this format?
You can verify your input using regex:
/^\d+(,\d+)*$/.test(value)

javascript validation for input time in format hh:mm a

I want to make user enter time in format "hh:mma" or "hh:mm a". I want to leave it to user the choice to enter space between mm & a.
What can be the regular expression for this ?
Use ? to make the preceding token optional, for example /colou?r/ matches color and colour. If you are struggling with the hh:mm part of the regex, this may help.
Try this:
(([0-1]?[0-9])|2[0-3]):[0-5][0-9] ?(am|pm|AM|PM)
Something like the following should work
([01]?[0-9]|2[0-3]):([0-5]?[0-9])\s?(am|pm)
I just put space in the pattern where I required space and if worked perfectly

javascript textbox not to allow special characters

I have a problem in validating Textbox in which I have to enter time.
in that only the symbol
":" is allowed,
text also should not allow.
how to do this..
and
I am following the pattern /^\d{1,2}:\d{2}$/
if anyone help me would be appreciated.
thanx in advance.
try and use following regular expression for validating HH:MM time format
^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$
try below pattern:
/^\d{2,}:\d{2}:\d{2}$/

Javascript regular expression does not match till the end of the string

I am validating dates using regular expression in javascript. The regular expression I am using is
/^(((((0?[1-9])|(1\d)|(2[0-8]))\/((0?[1-9])|(1[0-2])))|((31\/((0?[13578])|(1[02])))|((29|30)\/((0?[1,3-9])|(1[0-2])))))\/((20[0-9][0-9])|(19[0-9][0-9])))|((29\/02\/(19|20)(([02468][048])|([13579][26]))))$/
This matches dates accurately but it would match values such as
1/1/2001ff even though I am using $ to mark the end of string.
But if I give values like ff1/1/2001 it would invalidate it. So it's considering the start of the string and ignore the end of string part.
Does anyone know the reason for this.
From: Detecting an "invalid date" Date instance in JavaScript
if ( Object.prototype.toString.call(d) === "[object Date]" ) {
// it is a date
if ( isNaN( d.getTime() ) ) { // d.valueOf() could also work
// date is not valid
}
else {
// date is valid
}
}
else {
// not a date
}
Logically, it makes much more sense to check if the date is valid rather than using a regex to match a date. However, if you're trying to search for dates, your regex still works (I tested it in Notepad++ find for example.) Other than that, like the comment said, there's no reason to have such a complicated regex.
As correctly pointed out by Dracs, the issue was with missing brackets. Thank you very much for pointing that out.
The reason for not using javascript date object is we need only to allow mm/dd/yyyy format in the textbox. So it would be easy to use a regex to validate textbox.

Birthday input field - auto-adding dots after specific amount of characters

Hey guys,
unusual question: I have a input field #birthday that should accept european date format like this. dd.MM.yyyy
I wonder if it is possible to auto apply dots when typed into this field.
Imagine the inputfield is focused and empty. I start typing to numbers and the field autogenerates a dot, again two numbers and a dot is generated.
My aim is to type only numbers without needing to add the dots.
any idea how i could achieve that with jquery or javascript in general?
$('#birhtday').keypress(function() {
// ?
})
Why not use existing jQuery plugins?
Masked input
meioMask - I find it nicer because it doesn't use those underscores (but it seems to have some bugs when TABing through filled form)

Categories