469,275 Members | 1,709 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

need regex

Little astrology program has 2 text input boxes for birth dates. There are 3
selects for day month year that will supply output to whichever text input
had the focus last. If the user picks from the list boxes the relative text
input field is formatted d/m/y, the format that the cgi is expecting. The
user can also input directly to the text inputs and I would like to route
whatever the user inputs through a filter using regexes wherever possible.
I can do this without regex but it will take a ton of code. Any help will be
appreciated. TIA
Jimbo

regex wherever possible
first check for pair of separators. possibly . , - / \ 'space '

then check for valid values between the separators.
day >0 && day<32

If there are alpha chars in the month field run it through an array
(jan,feb,etc and try to get a month number
month>1 && month<12

if the year is four digits
year>1929 && year<2010
if year is two digits parse to above format

else don't submit help user :>)


Jul 23 '05 #1
5 1676
JRS: In article <41******@news.012.net.il>, dated Sat, 6 Nov 2004
08:06:29, seen in news:comp.lang.javascript, J. J. Cale
<ph****@netvision.net.il> posted :
Little astrology program has 2 text input boxes for birth dates. There are 3 sneerselects for day month year that will supply output to whichever text input
had the focus last. If the user picks from the list boxes the relative text
input field is formatted d/m/y, the format that the cgi is expecting. The
user can also input directly to the text inputs and I would like to route
whatever the user inputs through a filter using regexes wherever possible.
I can do this without regex but it will take a ton of code. Any help will be
appreciated. TIA


Read the newsgroup FAQ; see below.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #2

"Dr John Stockton" <sp**@merlyn.demon.co.uk> wrote in message
news:y8**************@merlyn.demon.co.uk...
JRS: In article <41******@news.012.net.il>, dated Sat, 6 Nov 2004
08:06:29, seen in news:comp.lang.javascript, J. J. Cale
<ph****@netvision.net.il> posted :
Little astrology program has 2 text input boxes for birth dates. There are 3
sneer
excuse me
selects for day month year that will supply output to whichever text inputhad the focus last. If the user picks from the list boxes the relative textinput field is formatted d/m/y, the format that the cgi is expecting. The
user can also input directly to the text inputs and I would like to route
whatever the user inputs through a filter using regexes wherever possible.I can do this without regex but it will take a ton of code. Any help will beappreciated. TIA


Read the newsgroup FAQ; see below.


Thank you. actually I have been to all of the below on several occasions.
I have no experience with regex. Unfortunaltely/or not I have'nt had to
make the effort. At age 67 it is a push. But thank you for your answer.
I scan google regularly for posts (Marin Honnen,Jukka,you) and regard
your advice highly.
Jimbo --
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 © <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources. <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items,

links.
Jul 23 '05 #3
J. J. Cale wrote:
The
user can also input directly to the text inputs and I would like to route
whatever the user inputs through a filter using regexes wherever possible.


See if the following can help you into building your script (slightly
tested only). Note that proposing two ways to input data could confuse
the user (not mentioning different date formats across the world);
moreover you'll still have to validate the input server-side.

HTH
<form action="foo.pl" onsubmit="return validate(this)">
<input type="text" name="ctrlDate" title="dd/mm/yyyy">
<input type="submit">
</form>

<script type="text/javascript">
function padLeft(str, pad, count) {
while(str.length<count)
str=pad+str;
return str;
}

function isValidDate(y, m, d){
var da=new Date(y, m, d);
return da.getFullYear()==y &&
da.getMonth()==m &&
da.getDate()==d;
}

function check(el, field) {
//controller
var result={errors:[]}, r;
for(var ii=2; ii<arguments.length; ii++) {
r=singleCheck(el, field, arguments[ii]);
for(var j=0; j<r.errors.length; j++)
result.errors[result.errors.length]=r.errors[j];
}
return result;

//single check
function singleCheck(el, field, type) {
var result={errors:[]};
switch(type) {
case "date" :
var reDate, match, year, month, day;

reDate=
/^(\d{1,2})([-.,\/\\ ])(\d{1,2}|\w{3})(\2)(\d{1,2}|\d{4})$/i;

if((match=reDate.exec(el.value))) {
//store values
year=match[5];
month=match[3];
day=match[1];

//convert the month if needed
if(/\D/.test(month))
month={
jan:"1", feb:"2", mar:"3", apr:"4", may:"5", jun:"6",
jul:"7", aug:"8", sep:"9", oct:"10", nov:"11", dec:"12"
}[month.toLowerCase()]||"0";
if(+month<1 || +month>12)
result.errors[result.errors.length]=
"Field \""+field+"\" : month not recognised";

//convert the year if needed
if(year.length==1) year="0"+year;
if(year.length==2) year=((+year<=10) ? "20":"19") + year;
if(+year<1929 || +year>2010)
result.errors[result.errors.length]=
"Field \""+field+"\" : "+
"year should be between 1929 and 2010";

///validate the date
if(result.errors.length==0) {
if(!isValidDate(year, month-1, day)) {
result.errors[result.errors.length]=
"Field \""+field+"\" : invalid date found";
} else {
//format the input
el.value = padLeft(day,"0",2) + "\/" +
padLeft(month,"0",2) + "\/" +
year;
}
}
} else {
//format not correct
result.errors[result.errors.length]=
"Field \""+field+"\" : invalid format (try dd/mm/yyyy)";
}
break;
}
if(el.style)
el.style.borderColor = result.errors.length>0 ? "#c00" : "";

return result;
}
}

function validate(form){
var a=[], msg=[], index=1;

a[a.length]=check(form.elements["ctrlDate"], "date", "date");
for(var ii=0; ii<a.length; ii++)
if(a[ii].errors.length>0)
for(var j=0;j<a[ii].errors.length; j++)
msg[msg.length]=(index++)+" - "+a[ii].errors[j];
if(msg.length) alert(msg.join("\n"));
return false; //!msg.length;
}
</script>
Jul 23 '05 #4

"Yann-Erwan Perio" <y-*******@em-lyon.com> wrote in message
news:41***********************@news.free.fr...
J. J. Cale wrote:
The
user can also input directly to the text inputs and I would like to route whatever the user inputs through a filter using regexes wherever
possible.
See if the following can help you into building your script (slightly
tested only). Note that proposing two ways to input data could confuse
the user (not mentioning different date formats across the world);
client wants a mouse option and suggested pick lists
moreover you'll still have to validate the input server-side.


the cgi will parse any valid date string. Valid being of one of the formats
d/m/yyyy d/mm/yyyy dd/m/yyyy dd/mm/yyyy
where d>0&&d<32 m>0&&m<13 y>1929&&y<2009

YEP hi
You are magic. Saved your code. Thank you! I am still hoping to do this
with minimum code and avoid using a date object at all costs. The example
below "seems" to be working ok in IE6. Except for the event handler is there
anything that won't work in Mozilla or others?
An hour ago I had no idea how to use a regular expression.
Here is what I have after an hour of scanning Dr Stocktons examples, and is
more what I hoped to achieve and more than I dreamed I'd be able to do. And
I'll bet you or others can sophisticate my basic idea down to 10 lines or
less. I'll be happy until someone points out all the holes there probably
are. Thanks again.
Jimbo
<HTML><HEAD><TITLE>Validate date format</TITLE>
<script type="text/javascript">
function checkChar() {
// only accept numbers and the backslash
// I know! IE specific :>[
var char = String.fromCharCode(event.keyCode);
if(!/\d|\//.test(char)) {
alert("enter a number from 0 - 9 \n or a backslash /");
event.returnValue = false;
}
}
function checkFormat(obj) {
// d/m/yyyy d 1-9 , m 1-9
var a = /^[1-9]\/[1-9]\/\d{4}$/.test(obj.value);
// d/mm/yyyy d 1-9 , m 10-12
var b = /^[1-9]\/[1][0-2]\/\d{4}$/.test(obj.value);
// dd/m/yyyy d 10-29, m 1-9
var c = /^[1-2][0-9]\/[1-9]\/\d{4}$/.test(obj.value);
// dd/m/yyyy d 30-31, m 1-9
var d = /^[3][0-1]\/[1-9]\/\d{4}$/.test(obj.value);
// dd/mm/yyyy d 10-29, m 10-12
var e = /^[1-2][1-9]\/[1][0-2]\/\d{4}$/.test(obj.value);
//dd/mm/yyyy d 30-31, m 10-12
var f = /^[3][0-1]\/[1][0-2]\/\d{4}$/.test(obj.value);
if(a || b || c || d || e || f) {
var y =
/^[1][9][3-9][0-9]$/.test(obj.value.substr(obj.value.lastIndexOf("/")+1));
var z =
/^[2][0][0][0-9]$/.test(obj.value.substr(obj.value.lastIndexOf("/")+1));
alert(y || z) ; // if true submit.
}
}
</script></HEAD>
<BODY><center>
<form><input type="text" onkeypress = "checkChar()"
onchange="checkFormat(this)"></form>
</center></BODY></HTML>
Jul 23 '05 #5
JRS: In article <41********@news.012.net.il>, dated Sun, 7 Nov 2004
21:08:24, seen in news:comp.lang.javascript, J. J. Cale
<ph****@netvision.net.il> posted :

"Yann-Erwan Perio" <y-*******@em-lyon.com> wrote in message
news:41***********************@news.free.fr...
J. J. Cale wrote:
> The
> user can also input directly to the text inputs and I would like toroute > whatever the user inputs through a filter using regexes wherever
possible.

See if the following can help you into building your script (slightly
tested only). Note that proposing two ways to input data could confuse
the user (not mentioning different date formats across the world);


client wants a mouse option and suggested pick lists
moreover you'll still have to validate the input server-side.


the cgi will parse any valid date string. Valid being of one of the formats
d/m/yyyy d/mm/yyyy dd/m/yyyy dd/mm/yyyy
where d>0&&d<32 m>0&&m<13 y>1929&&y<2009


It's not obvious what application can require such a date range, apart
from one connected with employment, in which case starting with 1930
seems still risky - how old is Mr Sharon these days? Many of us will
have living relatives born before 1930.

ISTM likely that the client will need to update the page before 2009;
you could instead allow a dynamic date range, from the current year (or
day) back almost 100 years.

I am still hoping to do this with minimum code and avoid using a date
object at all costs.
Those are incompatible aims, at least if you would like to validate the
dates properly. A Date Object is very effective at date validation,
although one can do it much faster by well-considered other means.

An hour ago I had no idea how to use a regular expression.
Here is what I have after an hour of scanning Dr Stocktons examples, and is
A few more hours should do it, then.
more what I hoped to achieve and more than I dreamed I'd be able to do. And
I'll bet you or others can sophisticate my basic idea down to 10 lines or
less. I'll be happy until someone points out all the holes there probably
are.
function checkChar() {
// only accept numbers and the backslash
// I know! IE specific :>[
var char = String.fromCharCode(event.keyCode);
if(!/\d|\//.test(char)) {
alert("enter a number from 0 - 9 \n or a backslash /");
event.returnValue = false;
}
}
There is no need to check the characters in that manner if the data will
be properly checked with a RegExp.

function checkFormat(obj) {
// d/m/yyyy d 1-9 , m 1-9
var a = /^[1-9]\/[1-9]\/\d{4}$/.test(obj.value);
// d/mm/yyyy d 1-9 , m 10-12
var b = /^[1-9]\/[1][0-2]\/\d{4}$/.test(obj.value);
// dd/m/yyyy d 10-29, m 1-9
var c = /^[1-2][0-9]\/[1-9]\/\d{4}$/.test(obj.value);
// dd/m/yyyy d 30-31, m 1-9
var d = /^[3][0-1]\/[1-9]\/\d{4}$/.test(obj.value);
// dd/mm/yyyy d 10-29, m 10-12
var e = /^[1-2][1-9]\/[1][0-2]\/\d{4}$/.test(obj.value);
//dd/mm/yyyy d 30-31, m 10-12
var f = /^[3][0-1]\/[1][0-2]\/\d{4}$/.test(obj.value);
if(a || b || c || d || e || f) {
var y =
/^[1][9][3-9][0-9]$/.test(obj.value.substr(obj.value.lastIndexOf("/")+1));
var z =
/^[2][0][0][0-9]$/.test(obj.value.substr(obj.value.lastIndexOf("/")+1));
alert(y || z) ; // if true submit.
}
}


Consider :

function CF(Ob) { var S, Y, M, D
if (!/^\d\d?\/\d\d?\/\d{4}$/.test(Ob.value)) return false // #
S = Ob.value.split("/")
Y = +S[2] ; M = S[1]-1 ; D = +S[0]
if (Y<1930 || Y>2008) return false
with (new Date(Y, M, D)) return getMonth()==M && getDate()==D
}

That accepts d[d]/m[m]/yyyy and checks for representing an actual date
in the range.

ISTM that almost anything plausible that the RegExp would reject would
otherwise be rejected subsequently.

The RegExp guarantees that Y will be a number; otherwise, one might wish
to rearrange the tests on Y so as to give the desired result if Y is
NaN.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Michael R. Pierotti | last post: by
1 post views Thread by hillcountry74 | last post: by
9 posts views Thread by jmchadha | last post: by
6 posts views Thread by Extremest | last post: by
7 posts views Thread by Extremest | last post: by
3 posts views Thread by aspineux | last post: by
15 posts views Thread by morleyc | last post: by
3 posts views Thread by =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post: by
4 posts views Thread by Danny Ni | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.