how can i validate year textbox in javascript? - 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.

Related

Regular Expression to validate if user has provided the correct URI format or not

I am trying to write a regular expression or regex for my HTML input field with text type. The user should enter the URI in the input field. The URI would look something like this: urn:URNNamespace:**:class:ObjClassid.
Some of the examples of valid URI are as follows:
urn:abc:testing:pqrs:1234556.1244
urn:wxyz:testing123:abc:1234556.*
urn:global:standard:value:myvalue
I was trying to check only if the initial characters are URN using the expression ^(urn):// and check if the string contains the character : in it.
I just want to make sure that user enters valid URN similar to the one that's provided. Is there any better way I can use and achieve this?
I would recommend a really great tool called Regulex that helps you build regular expressions.
I tried to create the described pattern and ended up with:
^urn:\w+:\w+:\w+:\d+(\.\d+)?$
You can try and change it here

How to use jQuery input mask plugin with special character regex validation?

I want to utilize inputmask plugin for special character & maxlength validation for text box. It supports either one. Is there a way to implement this?
var pattern = /^[\w\s\/\&\'\-]*$/i;
$("#example1").inputmask("99-9999999");
$("#example2").inputmask(pattern);

validation in javascript for escape sequence

I have textBox which accept alphabeti want to validate the textBox contain proper Drive Path or not using javascript
ex:Suppose Textbox contain 'D:\' then it's valid or else it's invalid...I need to check textbox contain ':\' or not after alphabet
plz help me
try to use javascript method "replace" http://www.w3schools.com/jsref/jsref_replace.asp to remove unwanted contents.
You can use following regex
/^(\\(\\[^\s\\]+)+|([A-Za-z]:(\\)?|[A-z]:(\\[^\s\\]+)+))(\\)?$/

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

jQuery - check proper time format

I have an input field where the user enters a time in format mm:hh. The format is specified inside the field (default value 09:00) but I still want to perform a client-side check to see if the format is correct.
Since the existing client-side validation is in jQuery, I'd like to keep this in jQuery as well.
I'm mainly a PHP programmer so I need some help writing this one in an optimal manner.
I know I can check each individual character (first two = digits, third = ':', last two = digits) but is there a way to do it more elegantly, while also checking the hour count is not larger than 23 and the minute count isn't larger than 59?
In PHP I would use regular expressions, I assume there's something similar for jQuery?
Something like this makes sense to me:
([01]?[0-9]|2[0-3]):[0-5][0-9]
But I'm not too familiar with jQuery syntax so I'm not sure what to do with it.
you can use regex in JavaScript too:
http://www.w3schools.com/jsref/jsref_obj_string.asp
use
.search() - http://www.w3schools.com/jsref/jsref_search.asp
or .match() - http://www.w3schools.com/jsref/jsref_match.asp
or .replace() - http://www.w3schools.com/jsref/jsref_replace.asp
EDIT:
<script>
var regexp = /([01][0-9]|[02][0-3]):[0-5][0-9]/;
var correct = ($('input').val().search(regexp) >= 0) ? true : false;
</script>
EDIT2:
here the documentation of regexpobject in javascript:
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
<script>
var regexp = /([01][0-9]|[02][0-3]):[0-5][0-9]/;
var correct = regexp.test($('input').val());
</script>
EDIT3:
fixed the regexp
In JavaScript, regexes are delimited by /. So you would do...
var isValidTime = /([01]?[0-9]|2[0-3]):[0-5][0-9]/.test(inputString);
Your regex seems to make sense for validating any 24H time in HH:mm format. However as I state in my comment under Andreas's answer - this will return true if any part of the string matches. To make it validate the entire string, use anchors to match the start and end of the string also eg...
var isValidTime = /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/.test(inputString);
To actually pull the matches you would need to use the string.match JS function.
Oh - and please be aware that jQuery is Javascript - it's just a library of very useful stuff! However this example contains no reference to the jQuery library.
In JavaScript you can use yourstring.match(regexp) to match a string against a regular expression.
I have very limited RegEx-experience, so I cant help you with the pattern, but if you have that set .match() should be all it takes.
A different approach, but using an extra javascript lib instead of regular expressions:
var valid = moment(timeStr, "HH:mm", true).isValid();
I guess if you already use moment.js in your project, there's no downside.
And since you didn't specifically request an answer using regular expressions, I think it's valid to mention.

Categories