473,320 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1914
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Joe | last post by:
Hi, I have been using a regular expression that I don’t uite understand to filter the valid email address. My regular expression is as follows: <asp:RegularExpressionValidator...
2
by: Michael R. Pierotti | last post by:
Dim reg As New Regex("^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$") Dim m As Match = reg.Match(txtIPAddress.Text) If m.Success Then 'No need to do anything here Else MessageBox.Show("You need to enter a...
1
by: hillcountry74 | last post by:
Hi, I'm stuck with this regular expression from past 2 days. Desperately need help. I need a regular expression that will allow all characters except these *:~<>' This is my code in...
9
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat...
6
by: Extremest | last post by:
I have a huge regex setup going on. If I don't do each one by itself instead of all in one it won't work for. Also would like to know if there is a faster way tried to use string.replace with all...
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
3
by: aspineux | last post by:
My goal is to write a parser for these imaginary string from the SMTP protocol, regarding RFC 821 and 1869. I'm a little flexible with the BNF from these RFC :-) Any comment ? tests= def...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
3
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
I'm trying to learn regex but since I've spent way too much time on the following "simple" case, there's obviously something I'm missing. I need to find all occurrences of a specific...
4
by: Danny Ni | last post by:
Hi, The following code snippet is causing CPU to max out on my local machine and production servers. It looks fine on Expresso though. Regex rgxVideo = new...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.