regex help
-
I want to force users to enter dates in a textbox as mm/yyyy The following works:
<html>
<head>
<title></title>
<script type="text/javascript" >
function testFormat(v) {
//var re = new RegExp("^([1-9]|1[0-2]|0[1-9])\/(\d{4})$");
var re = new RegExp(document.getElementById("txtR").value);
if (v.match(re) && (v.length==7)) {alert('ok');} else { alert('oops');}
}
</script>
</head>
<body ><form runat="server" id="Form1">
<input type="text" id="txtR" value="^([1-9]|1[0-2]|0[1-9])\/(\d{4})$" style="width:200px;" />
<br />
<input type="text" id="txtA" onblur="testFormat(this.value)" style="width:100px;" />
</form></body>
</html>However: 1. Why does the commented out javascript line not work? Instead I have to use this silly workaround of putting the regular expression in a text field, (well, I can make this hidden) and reference that. It's the only way I can make it work - but it's daft. 2. In order to force a 2-digit month, I have added the test for v.length==7 - but it must be possible to amend the regex to check for this? Damned if I can see how...
Check here: http://regexper.com/#%5E(%5B1-9%5D%7C1%5B0-2%5D%7C0%5B1-9%5D)%2F(%5Cd%7B4%7D)%24[^] You have a lonely / in your regex that causes a syntax error... (Use this site/tool to visually check your regex in the future :-))
Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.
-
Check here: http://regexper.com/#%5E(%5B1-9%5D%7C1%5B0-2%5D%7C0%5B1-9%5D)%2F(%5Cd%7B4%7D)%24[^] You have a lonely / in your regex that causes a syntax error... (Use this site/tool to visually check your regex in the future :-))
Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.
Ta - Not being funny, but you didn't have a grandad (?) who was a Latin teacher by any chance did you? I had a Peter Kornfield teaching that to me way back in the 60's/70's..... (least, I'm pretty sure it was 'Peter'.) He was an alright bloke...
-
Ta - Not being funny, but you didn't have a grandad (?) who was a Latin teacher by any chance did you? I had a Peter Kornfield teaching that to me way back in the 60's/70's..... (least, I'm pretty sure it was 'Peter'.) He was an alright bloke...
I definitely had a grandfather :-D , but he was a travelling salesman of textiles...However I'm not sure he ever got to the UK, and in the 60s/70s he was clsoe to 60/70 as he was born in 1904... (What funny is that as a travelling salesman he spoke 7 languages - not including Latin, which he has learned in school but never used)
Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.
-
I definitely had a grandfather :-D , but he was a travelling salesman of textiles...However I'm not sure he ever got to the UK, and in the 60s/70s he was clsoe to 60/70 as he was born in 1904... (What funny is that as a travelling salesman he spoke 7 languages - not including Latin, which he has learned in school but never used)
Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.
Yeah, well I had two!! ;P OK, not him then. He was interesting - he had a gammy leg as a resuilt of some Nazi 'experiments' which he was unfortunate enough to be caught up in, but fortunate enough to survive. IDK the full story, being only a schoolboy at the time. But he had to spray some kind of medication on it even all these years later which used to stink to high heaven. But we all got used to it. He actually died in my last year there. Never forget his lessons, though can't say I've had much use for Latin either! Suppose it's sort of useful sometimes thinkng about the etymology of words....
-
Yeah, well I had two!! ;P OK, not him then. He was interesting - he had a gammy leg as a resuilt of some Nazi 'experiments' which he was unfortunate enough to be caught up in, but fortunate enough to survive. IDK the full story, being only a schoolboy at the time. But he had to spray some kind of medication on it even all these years later which used to stink to high heaven. But we all got used to it. He actually died in my last year there. Never forget his lessons, though can't say I've had much use for Latin either! Suppose it's sort of useful sometimes thinkng about the etymology of words....
Wombaticus wrote:
resuilt of some Nazi 'experiments'
My grandfather too survived one of those horrible things, they called it Auschwitz...
Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.
-
I want to force users to enter dates in a textbox as mm/yyyy The following works:
<html>
<head>
<title></title>
<script type="text/javascript" >
function testFormat(v) {
//var re = new RegExp("^([1-9]|1[0-2]|0[1-9])\/(\d{4})$");
var re = new RegExp(document.getElementById("txtR").value);
if (v.match(re) && (v.length==7)) {alert('ok');} else { alert('oops');}
}
</script>
</head>
<body ><form runat="server" id="Form1">
<input type="text" id="txtR" value="^([1-9]|1[0-2]|0[1-9])\/(\d{4})$" style="width:200px;" />
<br />
<input type="text" id="txtA" onblur="testFormat(this.value)" style="width:100px;" />
</form></body>
</html>However: 1. Why does the commented out javascript line not work? Instead I have to use this silly workaround of putting the regular expression in a text field, (well, I can make this hidden) and reference that. It's the only way I can make it work - but it's daft. 2. In order to force a 2-digit month, I have added the test for v.length==7 - but it must be possible to amend the regex to check for this? Damned if I can see how...
Although you've got a solution, I can't obviously see that anyone's given you an explanation why the commented out line doesn't work. :)
var re = new RegExp("^([1-9]|1[0-2]|0[1-9])/(\d{4})$");
In Javascript strings[^], the backslash (
\
) is used to escape the following character. As a result, your pattern actually comes out as:^([1-9]|1[0-2]|0[1-9])/(d{4})$
That's looking for the literal character "
d
", not the digits character class "\d
". You can solve it by either escaping the backslash within the string:var re = new RegExp("^([1-9]|1[0-2]|0[1-9])/(\\d{4})$");
or using a regular expression literal:
var re = /^([1-9]|1[0-2]|0[1-9])\/(\d{4})$/;
NB: For a regular expression literal you have to escape the forward-slash (
/
) character, since that's also used to terminate the literal.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Although you've got a solution, I can't obviously see that anyone's given you an explanation why the commented out line doesn't work. :)
var re = new RegExp("^([1-9]|1[0-2]|0[1-9])/(\d{4})$");
In Javascript strings[^], the backslash (
\
) is used to escape the following character. As a result, your pattern actually comes out as:^([1-9]|1[0-2]|0[1-9])/(d{4})$
That's looking for the literal character "
d
", not the digits character class "\d
". You can solve it by either escaping the backslash within the string:var re = new RegExp("^([1-9]|1[0-2]|0[1-9])/(\\d{4})$");
or using a regular expression literal:
var re = /^([1-9]|1[0-2]|0[1-9])\/(\d{4})$/;
NB: For a regular expression literal you have to escape the forward-slash (
/
) character, since that's also used to terminate the literal.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thank you!
-
Try this:
<script type="text/javascript">
function testFormat(v) {
var re = new RegExp(/^(1[0-2]|0[1-9])[/]{1}([2-9]\d[1-9]\d|[1-9]\d)$/);
if (v.match(re)) { alert('ok'); } else { alert('oops'); }
}
</script>Should also take care of your "7" problem
Hi umm.. hate to say this, but this doesn't actually work!!! 12/2015 = OK, but 12/1995 = false ?? 12/2000 = false ?? any deas..?
-
Hi umm.. hate to say this, but this doesn't actually work!!! 12/2015 = OK, but 12/1995 = false ?? 12/2000 = false ?? any deas..?
-
Hi umm.. hate to say this, but this doesn't actually work!!! 12/2015 = OK, but 12/1995 = false ?? 12/2000 = false ?? any deas..?
-
for 12/1995, you see where it says: [2-9] - change it to [1-9] It was assuming 2000+ 12/2000 - just a sec... Let me check it out...
Ta - that bit works.. For the other - it doesn't seem to like a double-0 ...
-
Less restrictive:
<script type="text/javascript">
function testFormat(v) {
var re = new RegExp(/^(1[0-2]|0[1-9])[/]{1}(\d{4})$/);
if (v.match(re)) { alert('ok'); } else { alert('oops'); }
}</script>
:thumbsup: Thanks ever so