The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

Apparently India has not learned about Regular Expressions yet

Last post 01-24-2008 11:53 AM by derula. 42 replies.
Page 1 of 1 (43 items)
Sort Posts: Previous Next
  • 01-17-2008 1:32 PM

    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

     

  • 01-17-2008 1:41 PM In reply to

    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
  • 01-17-2008 1:42 PM In reply to

    • Steeldragon
    • Top 500 Contributor
    • 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
    Filed under:
  • 01-17-2008 1:54 PM In reply to

    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;
    }


     

  • 01-17-2008 2:08 PM In reply to

    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
  • 01-17-2008 2:09 PM In reply to

    • dlikhten
    • Top 25 Contributor
    • 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 Earth
    SSDS Bug: Program should not start up
  • 01-17-2008 2:10 PM In reply to

    • trwww
    • Not Ranked
    • 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.
    Filed under:
  • 01-17-2008 2:49 PM In reply to

    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!
  • 01-17-2008 3:23 PM In reply to

    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... --
  • 01-17-2008 3:32 PM In reply to

    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
  • 01-17-2008 4:55 PM In reply to

    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."
  • 01-17-2008 5:44 PM In reply to

    • wgh
    • Top 500 Contributor
    • 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."
  • 01-17-2008 10:42 PM In reply to

    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.
     

  • 01-18-2008 2:27 AM In reply to

    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
     

  • 01-18-2008 3:30 AM In reply to

    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.
  • 01-18-2008 7:23 AM In reply to

    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.

  • 01-18-2008 9:06 AM In reply to

    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."
  • 01-18-2008 9:31 AM In reply to

    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."
  • 01-18-2008 9:46 AM In reply to

    • XIU
    • Top 200 Contributor
    • 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
  • 01-18-2008 10:19 AM In reply to

    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?
  • 01-18-2008 12:51 PM In reply to

    Re: Apparently India has not learned about Regular Expressions yet

    "a suspected numeric input"

     hilarious

  • 01-18-2008 3:43 PM In reply to

    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.
  • 01-18-2008 8:38 PM In reply to

    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? 

  • 01-18-2008 8:47 PM In reply to

    • tster
    • Top 10 Contributor
    • Joined on 04-11-2006
    • Natick, MA
    • Posts 1,286

    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.
  • 01-19-2008 1:02 AM In reply to

    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.