javascript textbox not to allow special characters - javascript

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}$/

Related

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

jQuery - Regex to prevent any special characters

I'm using jQuery's validation plugin to validate a form.
I have a field which should only contain letters or numbers. I have tried the following regex but it's still allowing characters such as ' ; :
[a-zA-Z 0-9]$
What am I doing wrong?
Thanks in advance
Try this:
^[a-zA-Z 0-9]*$
If this is a required field, then change asterisk to plus:-
^[a-zA-Z 0-9]+$
Try this expression::
^[a-zA-Z 0-9]+$

Javascript RegExpression

I am looking for a regular expression that will accept date formats, both MM/DD/YYYY and M/D/YYYY
I found one here
However when I run that I get an unexpected illegal token. Here is how I am implementing the javascript function
return RegExp(/^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$/).test(txtDate);
I've been told the http://regexlib.com website usually works well for .net regular expressions, but not javascript.
Any help would be great!
Escape the slashes inside the regex.
/^(((0?[1-9]|1[012])\/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])\/(29|30)|(0?[13578]|1[02])\/31)\/(19|[2-9]\d)\d{2}|0?2\/29\/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$/
here __^ __^ __^ ___^ ___^___^
for MM/DD/YY /^(\d{1,2})[./-](\d{1,2})[./-](\d{4})$/
for M/D/Y /^(\d{1,2})[./-](\d{1,2})[./-](\d{2}|\d{4})$/
Recipe 4.4 from Regex Cookbook does most of what you are looking for:
^(1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2}$
Take a look at this:
http://jsfiddle.net/oscarj24/32JQr/
hope this helps

how can i validate year textbox in javascript?

I am using a textbox on my page which is used to enter year in yyyy-yyyy format.
Can you tell how can i validate textbox in this format through javascript??
Thanks in advance...
You can match it with a regular expression:
var pattern = /\d{4}-\d{4}/;
If you are not using any javascript library (like jQuery, etc), Javascript Regular Expressions could be used for validating the input.

Regex to validate textbox length

I have this RegEx that validates input (in javascript) to make sure user didn't enter more than 1000 characters in a textbox:
^.{0,1000}$
It works ok if you enter text in one line, but once you hit Enter and add new line, it stops matching. How should I change this RegEx to fix that problem?
The problem is that . doesn't match the newline character. I suppose you could use something like this:
^[.\r\n]{0,1000}$
It should work (as long as you're not using m), but do you really need a regular expression here? Why not just use the .length property?
Obligatory jwz quote:
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
Edit: You could use a CustomValidator to check the length instead of using Regex. MSDN has an example available here.
What you wish is this:
/^[\s\S]{0,1000}$/
The reason is that . won't match newlines.
A better way however is to not use regular expressions and just use <text area element>.value.length
If you just want to verify the length of the input wouldn't it be easier to just verify the length of the string?
if (input.length > 1000)
// fail validation

Categories