Skip to main content
Inspiring
June 2, 2009
Question

Implementing conditional inside UDF?

  • June 2, 2009
  • 2 replies
  • 677 views

Hello,

I have UDF I wrote for formatting phone numbers;

<cfscript>

/* Converts 800 555 1212 into (800) 555-1212*/
function phix(str)
{
    /*Get rid of the spaces, for now 8005551212*/
    VAR pn=ReReplaceNoCase(str,'[-, ,.,(,)]','','ALL');
    /*Insert Left Parenthesis in str*/
    VAR pn2=INSERT('(',pn,0);
    /*Insert Right Parenthesis in str*/
    VAR pn3=INSERT(')',pn2,4);
    /*Insert Space after Right Parenthesis in str*/
    VAR pn4=INSERT(" ",pn3,5);
    /*Insert Dash after Prefix in str*/
    VAR pn5=INSERT("-",pn4,9);
    RETURN pn5;
    }

</cfscript>

And I am wanting to apply it only if the value of the str input is numeric. The field in the form is not restrictive, nor should it be - things like "ext 123" or "daytime" might need to be input as well as the phone number.

So I am trying to add an IF IsNumeric(str) to the function so it will only execute if the input of str is a number string. So far none of the examples of conditionals within function scripts seems to work when I replicate their syntax. I'm sure it's a simple thing, hopefully one of you can help out.

So, to recap... If the input str is digits only, apply function, else do nothing to the str.

Thanks!

    This topic has been closed for replies.

    2 replies

    ilssac
    Inspiring
    June 3, 2009

    I beleive Mack gave you a good answer to your question.

    I just wanted to point out that you do NOT need to create a new local variable for every step of your function.   Probably not a big deal for a quick and simple function like this, but it is a bit of a wasted memory.

    You should be able to do something like this:

        /*Get rid of the spaces, for now 8005551212*/
        VAR pn=ReReplaceNoCase(str,'[-, ,.,(,)]','','ALL');
        /*Insert Left Parenthesis in str*/
        VAR pn=INSERT('(',pn,0);
        /*Insert Right Parenthesis in str*/
        VAR pn=INSERT(')',pn,4);
        /*Insert Space after Right Parenthesis in str*/
        VAR pn=INSERT(" ",pn,5);
        /*Insert Dash after Prefix in str*/
        VAR pn=INSERT("-",pn,9);
        RETURN pn;
    Inspiring
    June 16, 2009

    Ok, I see where mack is going, and the syntax is the thing screwing me up, actually. Also what I need to do is accept parens, hyphens, numbers and spaces and assume "phone number" and run the UDF. If there are any other characters ([a-z], etc)-or if the field is blank -  assume NOT a phone number and do not run UDF, just pass original string back.

    **Also when I used your single pn var example, I got the following error;

    Cannot declare local variable pn twice

    Perhaps the VAR doesn't belong infront of the subsequent evals?

    ilssac
    Inspiring
    June 16, 2009

    Yes, You only "var" a variable once at the top of a function.  Sorry I did not remove that when quicky modifying your original code.

        /*Get rid of the spaces, for now 8005551212*/
        VAR pn=ReReplaceNoCase(str,'[-, ,.,(,)]','','ALL');
        /*Insert Left Parenthesis in str*/
        pn=INSERT('(',pn,0);
        /*Insert Right Parenthesis in str*/
        pn=INSERT(')',pn,4);
        /*Insert Space after Right Parenthesis in str*/
        pn=INSERT(" ",pn,5);
        /*Insert Dash after Prefix in str*/
        pn=INSERT("-",pn,9);
        RETURN pn;
    Participating Frequently
    June 2, 2009

    Mack