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 :>) | | | | re: need regex
JRS: In article <418c69de@news.012.net.il>, dated Sat, 6 Nov 2004
08:06:29, seen in news:comp.lang.javascript, J. J. Cale
<photom@netvision.net.il> posted :[color=blue]
>Little astrology program has 2 text input boxes for birth dates. There are 3[/color]
sneer[color=blue]
>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[/color]
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. | | | | re: need regex
"Dr John Stockton" <spam@merlyn.demon.co.uk> wrote in message
news:y8hwD+GhtUjBFwC6@merlyn.demon.co.uk...[color=blue]
> JRS: In article <418c69de@news.012.net.il>, dated Sat, 6 Nov 2004
> 08:06:29, seen in news:comp.lang.javascript, J. J. Cale
> <photom@netvision.net.il> posted :[color=green]
> >Little astrology program has 2 text input boxes for birth dates. There[/color][/color]
are 3[color=blue]
> sneer[/color]
excuse me
[color=blue][color=green]
> >selects for day month year that will supply output to whichever text[/color][/color]
input[color=blue][color=green]
> >had the focus last. If the user picks from the list boxes the relative[/color][/color]
text[color=blue][color=green]
> >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[/color][/color]
possible.[color=blue][color=green]
> >I can do this without regex but it will take a ton of code. Any help will[/color][/color]
be[color=blue][color=green]
> >appreciated. TIA[/color]
>
> Read the newsgroup FAQ; see below.[/color]
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[color=blue]
> --
> © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE[/color]
4 ©[color=blue]
> <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of[/color]
news:comp.lang.javascript[color=blue]
> <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates,[/color]
sources.[color=blue]
> <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items,[/color]
links. | | | | re: need regex
J. J. Cale wrote:
[color=blue]
> 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.[/color]
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> | | | | re: need regex
"Yann-Erwan Perio" <y-e.perio@em-lyon.com> wrote in message
news:418dfe36$0$19893$626a14ce@news.free.fr...[color=blue]
> J. J. Cale wrote:
>[color=green]
> > The
> > user can also input directly to the text inputs and I would like to[/color][/color]
route[color=blue][color=green]
> > whatever the user inputs through a filter using regexes wherever[/color][/color]
possible.[color=blue]
>
> 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);[/color]
client wants a mouse option and suggested pick lists
[color=blue]
> moreover you'll still have to validate the input server-side.[/color]
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> | | | | re: need regex
JRS: In article <418e729f$1@news.012.net.il>, dated Sun, 7 Nov 2004
21:08:24, seen in news:comp.lang.javascript, J. J. Cale
<photom@netvision.net.il> posted :[color=blue]
>
>"Yann-Erwan Perio" <y-e.perio@em-lyon.com> wrote in message
>news:418dfe36$0$19893$626a14ce@news.free.fr...[color=green]
>> J. J. Cale wrote:
>>[color=darkred]
>> > The
>> > user can also input directly to the text inputs and I would like to[/color][/color]
>route[color=green][color=darkred]
>> > whatever the user inputs through a filter using regexes wherever[/color][/color]
>possible.[color=green]
>>
>> 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);[/color]
>
>client wants a mouse option and suggested pick lists
>[color=green]
>> moreover you'll still have to validate the input server-side.[/color]
>
>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[/color]
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.
[color=blue]
> I am still hoping to do this with minimum code and avoid using a date
> object at all costs.[/color]
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.
[color=blue]
>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[/color]
A few more hours should do it, then.
[color=blue]
>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.[/color]
[color=blue]
>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;
> }
>}[/color]
There is no need to check the characters in that manner if the data will
be properly checked with a RegExp.
[color=blue]
>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.
> }
>}[/color]
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. |  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,327 network members.
|