I have input field for year and I need a regex for validation it.
I have such code: ^([12]\d)?(\d\d)$.
But I want allow to validate only years in certain range (1990-2010, for example). How can I do it?
Edit. range must be 1950-2050
Try this:
1990 - 2010:
/^(199\d|200\d|2010)$/
1950 - 2050:
/^(19[5-9]\d|20[0-4]\d|2050)$/
Other examples:
1945 - 2013:
/^(194[5-9]|19[5-9]\d|200\d|201[0-3])$/
1812 - 3048:
/^(181[2-9]|18[2-9]\d|19\d\d|2\d{3}|30[0-3]\d|304[0-8])$/
Basically, you need to split your range into easy "regexable" chunks:
1812-3048: 1812-1819 + 1820-1899 + 1900-1999 + 2000-2999 + 3000-3039 + 3040-3048
regex: 181[2-9] 18[2-9]\d 19\d\d 2\d{3} 30[0-3]\d 304[0-8]
RegExp does not seem to be the right tool here. If you have the year values already isolated surely a simple comparison would work :
if (+yr >= 1990 && +yr <= 2010)
The +yr converts the string to a number
For a range from 1950 to 2050 you may use the following regex:
^19[5-9]\d|20[0-4]\d|2050$
Online demo
Regex:
/^(19[5-9]\d|20[0-4]\d|2050)$/
Easier...
var year = parseInt(textField.value, 10);
if( year >= 1950 && year <= 2050 ) {
...
}
(199[0-9]|200[0-9]|2010)
This will work in your 'example case'.
Helpful website: http://utilitymill.com/utility/Regex_For_Range
Regex for Current Year(Dynamic) from 1995
var current_year = new Date().getFullYear().toString().split("");
var reg=new RegExp("^(199[5-9]|200[0-9]|[0-"+current_year[0]+"][0-"+current_year[1]+"][0-"+current_year[2]+"][0-"+current_year[3]+"])$");
reg.test("1995");
if you want to check for age between for example 18 or 70 I did it like this.
my solution was
function yearRange(year) {
let now = new Date().getFullYear();
let age = now - Number(year);
if (age < 18 || age > 70) return false;
return true;
}
Here is a regex if you want to find a year in a film name for example.
Years b/n 1900 - 2029 and some symbols are allowed wrapping the year .-_+[(
(?<=(?:\s|\.|_|\-|\+|\(|\[))(?:19[2-9]|20[0-2])\d(?=\s|$|\.|_|\-|\+|\)|\])
check it out here
https://regex101.com/r/eQ9zK7/82
Note you can not start with year, because there is at least interval in front.
In the example first few lines are matching, because we have multiple lines in a single line they wont match.
1917.2019.1080p...
even if 1917 was in range it will mark only 2019
Related
This question already has answers here:
Regular expression for matching HH:MM time format
(22 answers)
Closed 3 years ago.
I'm using some masked input plugin, which supports mask as the RegEx array. In my case, I want to validate 24h time format (hh:mm). I'm using now RegEx like this:
[/^([0-2])/, /([0-9])/, ':', /[0-5]/, /[0-9]/]
First 2 array elements represents hours
/^([0-2])/, /([0-9])/
Any suggestions how to validate second hour digit? Cause now 25:00 is valid but shouldn't. Need somehow to check first number and if it's 2 - replace [0-9] with [0-3].
Something like this would be more flexible
const arr = ["07:00", "25:05", "23:59", "00:00"];
const valid = arr.filter(t => {
const parts = [hh, mm] = t.split(":");
return !isNaN(hh) && +hh >= 0 && +hh < 24 && !isNaN(mm) && +mm >= 0 && +mm < 60;
});
console.log(valid.length, arr.length, valid.length === arr.length)
I have a requirement to Validate a Decimal Field on an enterprise application to Hours and Minute Format.
so for example 7.30 is 7 hrs 30 minutes
9.55 is 9 hours 55minutes
10.80 .....this should not be accepted.
23.59 is the maximum.
I tried the sample code.
function ValidateTotalHours() {
var totalhours = Xrm.Page.getAttribute("new_totalhours").getValue();
if (!/^([0-23]).([0-5][0-9])$/.test(totalhours)) {
Xrm.Utility.alertDialog("Total Hours Format is invalid");
}
}
I tried validating with https://regex101.com/ before proceeding but it seems my Regex expression is incorrect.
Any ideas on the correct implementation.
Your pattern is incorrect:
[0-23] equals [0123]
. needs to be escaped. \. otherwise it will match any character except line break
What you need is: ^([0-1]?[0-9]|2[0-3])\.([0-5][0-9])$
const pattern = /^([0-1]?[0-9]|2[0-3])\.([0-5][0-9])$/;
const times = ['24.00', '23.60', '22.59', '04.05', '4.05', '23.02', '15.25', '24.59'];
times.forEach(time => {
console.log(`${time}: ${pattern.test(time)}`);
});
The [0-23] in the RegEx:
/^([0-23]).([0-5][0-9])$/
is actually specifying:
[0123]
You want something like this:
/^(2[0-3]|[01]?[0-9])\.[0-5][0-9]$/
This question already has answers here:
Password REGEX with min 6 chars, at least one letter and one number and may contain special characters
(10 answers)
Closed 5 years ago.
I know this has been asked a million times, but I just can't seem to crack it.
I have this:
function checkPassword(strPassword)
{
var objPattern = new RegExp("^.*(?=.{6,})(?=.*[a-z])[a-z0-9]*$");
var blnResult = objPattern.test(strPassword);
return(blnResult)
}
...but it only seems to check the length, and not if there's a number?
What have I missed?
Edit:
The number can be anywhere in the string, not necessarily at the end.
Keep it simple: if(strPassword.length >= 6 && /\d/.test(strPassword)) will do the work and is way more readable
If you need exactly 6 characters plus 1 number then you can use ^[A-z]{6}[0-9]{1}$ or like atleast 6 characters and atleast 1 number then use ^[A-z]{6,}[0-9]{1,}$
You can just include both tests separately in your function:
function checkPassword(strPassword){
var blnResult = /\w{6,}/.test(strPassword)
&& /\d+/.test(strPassword);
return(blnResult)
}
Demo:
function checkPassword(strPassword){
var blnResult = /\w{6,}/.test(strPassword)
&& /\d+/.test(strPassword);
return(blnResult)
}
var passwords = ["zeaezee2reer", "sds2", "ssdsdsdsdsd", "12155"];
passwords.forEach(function(p){
console.log(p+" ::: "+ checkPassword(p));
});
I have tried by using by splitting the experience and checking the value at first index of result and then need to validate that value also.
My code was like
experince = "2.02";
var exp = experince.split(".");
var exp_dec = parseInt(exp[1]);
if (exp_dec[0] == "0" || exp_dec[0] == "1"){
//validation successful;
}
I want to check for experience in years.
if experience is 3 year 4 month then it should allow 3.04
if experience is 1 year then it should be like 3.00
it should not contain any type of variable like "years"
it should not validate 1.3, 1.7 or 1.15,can only validate 1.10 or 1.01 or 1.07
You could use a regular expression like this:
var experienceFormat = /\d+\.((0\d)|(1[01]))/;
var result = experienceFormat.test('2.02');
https://jsfiddle.net/wcc5pmhe/
How to format Date in Angular Java script?
Code
<p id="summaryHeader">Calendar of {{dt}}</p>
I get the value as
2014-06-05T12:38:42.744Z
I tried this
<p id="summaryHeader">Calendar of {{dt|date:'MMMM dd'}}</p>
which gives me
Calendar of June 05
I need it as Calendar of June 05th or July 2nd and so on.. the rear rd,th,st is what I am looking for.
Anuglar Docs are good but don't specify this formatting.
I guess this is what you are looking for - http://www.michaelbromley.co.uk/blog/13/an-ordinal-date-filter-for-angularjs
A custom filter using the logic
app.filter('dateSuffix', function($filter) {
var suffixes = ["th", "st", "nd", "rd"];
return function(input) {
var dtfilter = $filter('date')(input, 'MMMM dd');
var day = parseInt(dtfilter.slice(-2));
var relevantDigits = (day < 30) ? day % 20 : day % 30;
var suffix = (relevantDigits <= 3) ? suffixes[relevantDigits] : suffixes[0];
return dtfilter+suffix;
};
});
And a Demo: http://plnkr.co/edit/HiyQ9uvxQL3FRoj7hKB8?p=preview
I wanted to have the ordinal indicator as a superscript, so used the following:
<div>{{amount}}<sup ng-bind="amount | ordinal"></sup></div>
Using the filter:
app.filter('ordinal', function() {
return function(number){
if (isNaN(number) || number < 1){
return '';
} else if (number % 100 == 11 || number % 100 == 12) {
return 'th';
} else {
var lastDigit = number % 10;
if (lastDigit === 1) {
return 'st';
} else if (lastDigit === 2) {
return 'nd';
} else if (lastDigit === 3) {
return 'rd';
} else if (lastDigit > 3) {
return 'th';
}
}
}
});
Note that this correctly renders 11th, 12th, 111th, 1012th, and not 11st.
You are correct that the date filter does not provide this formatting... I would suggest that you either write your own filter, or just do this:
Calendar of {{dt|date:'MMMM dd'}}{{getOrdinal(dt)}}
if you write your own, I would start with the one from angular as a baseline.
EDIT the idea of writing your own in the answer provided by guru is the approach I would take.
To expand upon the example he created, I would tweak it so that you can use this syntax:
Calendar of {{dt|date2:'MMMM ddoo'}}
Where oo is the ordinal suffix
I updated this representative plnkr to give you maximum flexibility.
For anybody that hits this question there is the project https://github.com/chrisiconolly/angular-all-ordinal-filters (full disclaimer: my own project) that will give you a ordinal number filter for angular.
Its used as so:
{{number | ordinal}} // 1 -> 1st
{{number | ordinalOnly}} // 1 -> st
It's fully tested and running through travis so it will remain stable.
You could wrap your date in a momentjs object like
$scope.dt = moment($scope.dt);
then do
<p id="summaryHeader">Calendar of {{dt.format("ddd Do")}}</p>
Other options may be better unless you already use momentjs elsewhere, just an option.
While not my best work you can start with this as a baseline
http://plnkr.co/edit/v2RuF72A9OPpFj5fvN8A?p=preview