|
Apparently India has not learned about Regular Expressions yet
Last post 01-24-2008 11:53 AM by derula. 42 replies.
-
01-17-2008 1:32 PM
|
|
-
Jonathan Holland


- Joined on 01-10-2008
- Posts 163
|
Apparently India has not learned about Regular Expressions yet
Doing some consulting work on a project that was developed overseas. I found this gem:
//Validates a phone number string. //phone numbers can be comma separated values. //these comma separated values can be in turn be - separated. //the numbers can be included in ()
function isValidPhoneNumber(phno) { var sphno= phno.split(","); var i=0; var j=0; for (i=0;i<sphno.length;i++) { temp=sphno[i]; //alert("sphno length"+sphno.length); //alert("comma separated value is"+temp+"and i is"+i); if(!isBlank(temp)) { if (!hasBalancedParanthesis(temp)) { alert("Phone number has unbalanced paranthesis"); return false; }//close of if else { eachNo= temp.split("-"); for (j=0;j<eachNo.length;j++) { //alert("eachNumber is"+eachNo[j]); if (!isBlank(eachNo[j])) { var reachedEnd=false; var k=0; var firstClosePar=0; var lastClosePar=0; while(!reachedEnd) { lastOpenPar=firstClosePar; var found=false; while (!found && k<eachNo[j].length) { if (eachNo[j].charAt(k)== '(') lastOpenPar=k; if (eachNo[j].charAt(k)==')') { firstClosePar=k; found=true; } k++; } //alert("came out of 1 st while and k is"+k); if(firstClosePar==lastOpenPar) firstClosePar=eachNo[j].length; var sNum=eachNo[j].substring(firstClosePar,lastOpenPar+1); //alert("snum "+sNum); if ( found && isBlank(sNum)) { //alert("came here"); alert("Phone number - Parenthesis must not be empty"); return false; } else { if (!isSpaceOrNumber1(sNum)) { //alert("eachNo"+eachNo[j]); // alert("sNumis"+sNum); alert("Phone number - Parenthesis must have valid numbers only"); return false; } } if(k>=eachNo[j].length-1) reachedEnd=true; }//end of while //alert("came out of 2 nd while loop"); }//end of if else{ alert("Invalid Phone Number"); return false; } }//end of for }//end of else }//end of if else { alert("Invalid Phone Number"); return false; } }//end of for //alert("was a valid phone number"); return true; }//end of function
|
|
-
-
belgariontheking


- Joined on 08-20-2007
- Cincinnati, OH, USA
- Posts 1,365
|
Re: Apparently India has not learned about Regular Expressions yet
In Javascript no less
I guess I'm back.
Please continue to spam the addresses below.
PLEASE SPAM: jtobin@ohioinstituteofhealthcareers.edu jtobin@ohiobusinesscollege.edu
|
|
-
-
Steeldragon


- Joined on 04-10-2006
- Where else of course but here
- Posts 88
|
Re: Apparently India has not learned about Regular Expressions yet
I know little about java or C but that to me looks like the spaghetti code i am forced to use to write programs on my graphing calculator that uses basic. maybe worse.
newfunction=noitcnufwen begin {{{If {Read(WTF)=True {ReturnValue(x<y<x)}}}} Or {If {Read(WTF)=False {ReturnValue(x>x+y,y>x,x<z^2,z<x,z>y)}}}} end
|
|
-
-
Jonathan Holland


- Joined on 01-10-2008
- Posts 163
|
Re: Apparently India has not learned about Regular Expressions yet
It gets better: //Validates Emailid //validates for "@" character and also atleast one "." character.
function isValidEmail(var1) { if(var1.value.length!=0) if(var1.value.indexOf('@')<1){ //alert("Invalid email-Id"); return false; //var1.value=""; //var1.focus(); } else{ if(var1.value.indexOf('.')>=(var1.value.length-1)) { return false; //alert("Invalid email-Id"); //var1.value=""; //var1.focus(); }else{ if(var1.value.lastIndexOf('.')<=(var1.value.indexOf('@'))){ return false; //alert("Invalid email-Id"); //var1.value=""; //var1.focus(); }else{ if(var1.value.lastIndexOf('@')!=(var1.value.indexOf('@'))){ //return false; //alert("Invalid email-Id"); //var1.value=""; //var1.focus(); } }//end of else }//end of if }//end of else return true; } And:
//To validate the date. //validates the date provided for being a valid date.
function isValidDate (year, month, day) { // Explicitly change type to integer to make code work in both // JavaScript 1.1 and JavaScript 1.2. if(!(isNumber(year)&&isNumber(month)&&isNumber(day)))return false; if ((month.length == 2) && (month.charAt(0) == "0")) month = month.charAt(1); if ((day.length == 2) && (day.charAt(0) == "0")) day = day.charAt(1);
var intYear = parseInt(year); var intMonth = parseInt(month); var intDay = parseInt(day); if(intDay<=0 || intMonth<=0 ||intYear<=0 ||intMonth>12) return false; // catch invalid days, except for February if (intDay > daysInMonth[intMonth]) return false; if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false; return true; }
// daysInFebruary (INTEGER year) // // Given integer argument year, // returns number of days in February of that year.
function daysInFebruary (year){ // February has 29 days in any year evenly divisible by four, // EXCEPT for centurial years which are not also divisible by 400. return ( ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 ); } And finally, why use that pesky isNaN method, when you can write your own: // general purpose function to see if a suspected numeric input // is a positive integer
function isNumber(inputStr) { for (var i = 0; i < inputStr.length; i++) { var oneChar = inputStr.substring(i, i + 1) if (oneChar < "0" || oneChar > "9") { return false; }//end of if }//end of for return true; }
|
|
-
-
Lysis


- Joined on 09-13-2007
- Posts 236
|
Re: Apparently India has not learned about Regular Expressions yet
Hey now, you don't expect Habib Abdul Deshucuiemenk to know our stinky American ways for his $.50/hour salary do ya? The injuns can write the codes just fine!
http://www.thebestpageintheuniverse.com
|
|
-
-
dlikhten


- Joined on 09-27-2007
- New York Citeyah
- Posts 670
|
Re: Apparently India has not learned about Regular Expressions yet
I just think they pay by the line. I mean no self-loving person would go though these pains when they can replace it with 1 or 2 lines of regex. I wonder if they code in notepad.
Code is like a box of chocolates. You never know who stuck a turd in there and why. The Stupidest Man On EarthSSDS Bug: Program should not start up
|
|
-
-
trwww


- Joined on 06-05-2007
- Posts 12
|
Re: Apparently India has not learned about Regular Expressions yet
Plz sirs email teh codes, I like them a lot. And how use.
|
|
-
-
El_Heffe


- Joined on 11-08-2007
- Posts 114
|
Re: Apparently India has not learned about Regular Expressions yet
trwww:Plz sirs email teh codes, I like them a lot. And how use.
ME TOO!
|
|
-
-
Outlaw Programmer


- Joined on 01-07-2008
- Posts 73
|
Re: Apparently India has not learned about Regular Expressions yet
My understanding was that a lot of these overseas developers Google pretty much every task. Guess they actually tried to work things out this time because the first search result that showed up for me was the regex one.
-- The sacrifice the code demanded... --
|
|
-
-
sootzoo


- Joined on 02-12-2007
- Posts 171
|
Re: Apparently India has not learned about Regular Expressions yet
Nice indentation level, 64 spaces or so? Makes me really *appreciate* its logical, erm, something.
All in all, this is either the stupidest thing I've read all week (and I'm porting SSDS to .NET!), or the worst trolling attempt ever. -bstorer
|
|
-
-
djork


- Joined on 09-28-2006
- Posts 647
|
Re: Apparently India has not learned about Regular Expressions yet
Jonathan Holland:And finally, why use that pesky isNaN method, when you can write your own:
Or when you can use the more appropriate (for outsourced workers) isNaaN method: function isNaaN(obj) { return obj.isBread && obj.isCookedInTandoor && obj.isDelicious }
"Frames securely mediate, by design. Secure multi-mediation is the future of all webbing."
|
|
-
-
wgh


- Joined on 07-07-2006
- Posts 58
|
Re: Apparently India has not learned about Regular Expressions yet
djork:
Or when you can use the more appropriate (for outsourced workers) isNaaN method:
function isNaaN(obj) { return obj.isBread && obj.isCookedInTandoor && obj.isDelicious }
me.isNowDesiringAGarlicNaan = true;
"You can't triple-stamp a double-stamp."
|
|
-
-
smxlong


- Joined on 06-23-2006
- Posts 37
|
Re: Apparently India has not learned about Regular Expressions yet
Okay, but let each of you ask yourself this question: "If I didn't have access to regular expressions, would my solution be any less fucked up than this one?" It doesn't look so bad. You people are a bunch of wimps. There are plenty of shops where no regex library is available or allowed to be used. Regular expressions are great, because they prevent EXACTLY this kind of code from having to be written. But it sounds like if you people didn't have them you'd be downright fucked. This code isn't bad.
|
|
-
-
DigitalXeron


- Joined on 06-16-2006
- Canada, ON
- Posts 79
|
Re: Apparently India has not learned about Regular Expressions yet
smxlong:Okay, but let each of you ask yourself this question: "If I didn't have access to regular expressions, would my solution be any less fucked up than this one?" It doesn't look so bad. You people are a bunch of wimps. There are plenty of shops where no regex library is available or allowed to be used. Regular expressions are great, because they prevent EXACTLY this kind of code from having to be written. But it sounds like if you people didn't have them you'd be downright fucked. This code isn't bad.
Then the shops are The Real WTF
|
|
-
-
dhromed


- Joined on 04-13-2005
- Dutchland
- Posts 2,614
|
Re: Apparently India has not learned about Regular Expressions yet
smxlong:Okay, but let each of you ask yourself this question: "If I didn't have access to regular expressions, would my solution be any less fucked up than this one?" It doesn't look so bad. You people are a bunch of wimps. There are plenty of shops where no regex library is available or allowed to be used. Regular expressions are great, because they prevent EXACTLY this kind of code from having to be written. But it sounds like if you people didn't have them you'd be downright fucked. This code isn't bad.
Name a language without regexp, save /VB[^(\.Net)]/i
There are plenty of shops where no regex library is allowed to be used
I severely doubt it.
But it sounds like if you people didn't have [regex] you'd be downright fucked
That might be true, though.
— Flurp.
|
|
-
-
TehFreek


- Joined on 07-12-2006
- Posts 42
|
Re: Apparently India has not learned about Regular Expressions yet
smxlong:Okay, but let each of you ask yourself this question: "If I didn't have access to regular expressions, would my solution be any less fucked up than this one?"
DFAs FTW.
|
|
-
-
djork


- Joined on 09-28-2006
- Posts 647
|
Re: Apparently India has not learned about Regular Expressions yet
smxlong:Okay, but let each of you ask yourself this question: "If I didn't have access to regular expressions, would my solution be any less fucked up than this one?"
It would absolutely be less fucked up. Scan through the input, extract each numeric digit. If you have enough digits, then you're done. Extension should be a separate field. Phone numbers should be stored as a simple string of digits, and then formatting can be applied later. I don't care if someone wants to input their snobbish "123.456.7890" style or a traditional "(123) 456-7890" or even "+11234567890."
smxlong:It doesn't look so bad. You people are a bunch of wimps. There are plenty of shops where no regex library is available or allowed to be used. Regular expressions are great, because they prevent EXACTLY this kind of code from having to be written. But it sounds like if you people didn't have them you'd be downright fucked. This code isn't bad.
I think it could at least be improved, with the original algorithm preserved, by reducing the "arrow code" going on here. The formatting is just absurd.
"Frames securely mediate, by design. Secure multi-mediation is the future of all webbing."
|
|
-
-
djork


- Joined on 09-28-2006
- Posts 647
|
Re: Apparently India has not learned about Regular Expressions yet
Here's a JS phone number validator: function isValidPhone(phoneStr) { return phoneStr.select(function(char) { return char.isDigit(); }).length >= minPhoneLength; }
String#select and String#isDigit are left as an exercise to the reader :)
"Frames securely mediate, by design. Secure multi-mediation is the future of all webbing."
|
|
-
-
XIU


- Joined on 01-08-2007
- Posts 134
|
Re: Apparently India has not learned about Regular Expressions yet
Exactly, loop through string, count digits, check if correct number of digits. Done
|
|
-
-
cconroy


- Joined on 08-18-2005
- NJ, USA
- Posts 366
|
Re: Apparently India has not learned about Regular Expressions yet
Yeah, that's some pretty awful indentation. I smell Notepad.
What Would Brian Boitano Do?
|
|
-
-
savar


- Joined on 08-02-2006
- Posts 289
|
Re: Apparently India has not learned about Regular Expressions yet
"a suspected numeric input"
hilarious
|
|
-
-
dhromed


- Joined on 04-13-2005
- Dutchland
- Posts 2,614
|
Re: Apparently India has not learned about Regular Expressions yet
djork:Scan through the input, extract each numeric digit. If you have enough digits, then you're done.
No. XIU:Exactly, loop through string, count digits, check if correct number of digits. Done
No.
— Flurp.
|
|
-
-
R.Flowers


- Joined on 12-20-2005
- Posts 611
|
Re: Apparently India has not learned about Regular Expressions yet
dhromed: djork:Scan through the input, extract each numeric digit. If you have enough digits, then you're done.
No.
Explain. dhromed: XIU:Exactly, loop through string, count digits, check if correct number of digits. Done
No.
Explain. Please?
|
|
-
-
tster


- Joined on 04-11-2006
- Natick, MA
- Posts 1,292
|
Re: Apparently India has not learned about Regular Expressions yet
R.Flowers: dhromed: djork:Scan through the input, extract each numeric digit. If you have enough digits, then you're done.
No.
Explain. dhromed: XIU:Exactly, loop through string, count digits, check if correct number of digits. Done
No.
Explain. Please?
please pass your algorithm over the following "phone number" e4e4e4e4e4e4e4e4e4e4 even worst, when you try to internationalize this code it will basically be writing a whole new function for each culture.
The pig go. Go is to the fountain. The pig put foot. Grunt. Foot in what? ketchup. The dove fly. Fly is in sky. The dove drop something. The something on the pig. The pig disgusting... see bio for the earth shattering ending.
|
|
-
-
bighusker


- Joined on 01-20-2007
- Posts 57
|
Re: Apparently India has not learned about Regular Expressions yet
TehFreek: smxlong:Okay, but let each of you ask yourself this question: "If I didn't have access to regular expressions, would my solution be any less fucked up than this one?"
DFAs FTW.
Quoted for Truth. A DFA is trivial to implement in code.
|
|
|
|