date, phone and zip edits

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ljungers
    New Member
    • Dec 2006
    • 114

    date, phone and zip edits

    Hi to all. PHP newbe here.

    Would like to know what is the simplest way to edit/validate dates, phone numbers and zip codes that are entered on a form. This data will be saved in a MySQL database if these fields contain valid formated data that is within allowable ranges.

    Are there examples that I could get of these editing functions or how to code for this type of edits.

    Thanks for any help in advance.
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Simplest is to use regular expressions. HOWEVER, you have to know what format you want to use. I.e. a US telephone no is quite different from a UK or a German telephone no. Same applies to ZIP or postal codes and date formats (US, ISO, etc.). So first decide on the required format, then we can discuss the regular expressions that should be used.

    Ronald :cool:

    Comment

    • ljungers
      New Member
      • Dec 2006
      • 114

      #3
      Thanks for a reply. As to formats they all will be in US format. Dates will eiter be in mmddyy or mmddyyyy format. Phone number I know that input wise I have seen 3 formats allowed like (123) 123-1234, 123-123-1234, 1231231234. Zip will be zip +4 allowed if they have the +4.

      Can multi formats be allowed plus edited or is it best to stick to one format allowed. Have been told that I should remove the - / ( ) from these fields prior to storing them in MySql. Is that true or just one way of storing data.

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        A bit of searching on the web resulted in these:
        [php]Matching US phone numbers in the following formats:

        ###-###-####
        (###) ###-####
        ##########

        Restricts area codes to >= 200 and exchanges to >= 100, since values below these are invalid.
        $pattern = "/(\([2-9]\d{2}\)\s?|[2-9]\d{2}-|[2-9]\d{2})"
        . "[1-9]\d{2}"
        . "-?\d{4}/";[/php]
        [php]US zip pattern 5 digits (01234) or 5 digits + 4 (01234-1234)

        $pattern = '^[0-9]{5}([- /]?[0-9]{4})?$^';[/php]

        Ronald :cool:

        Comment

        • ljungers
          New Member
          • Dec 2006
          • 114

          #5
          Thanks for the info & code. Would the date edits be simiular and I forgot to ask about decimal/number and currency edit's. The decimal or number fileds would be for number of months or number of persons. The currency would be for dollar amounts 9,999,999.99, 9,999,999.99-, or 9999999.99, 9999999.99-

          Thank you and will check back tomorrow

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Forgot the US dates
            Code:
            // US date formats MMDDYY and MMDDYYYY
            $pattern = '^(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])(19|20)?\d\d^';
            and the US currency[php]$tests = array(" $ 42.52 ",
            "$ 4232395",
            "4112412",
            "239583.52" ,
            "$ 3223.23",
            "$123,234.5 3",
            "$123,235",
            "$ 123,234,325.23" );
            $pattern = '/^\s*[$]?\s*((\d+)|(\d{ 1,3}(\,\d{3})+) )(\.\d{2})?\s*$/';
            foreach ($tests as $test) {
            // US currency
            if (preg_match($pa ttern, $test)) {
            echo "Matched on $test<br />";
            }
            else {
            echo "Failed match on $test<br />";
            }
            }
            ?>[/php]
            Ronald :cool:

            Comment

            • ljungers
              New Member
              • Dec 2006
              • 114

              #7
              Thank you for your help.

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                You are welcome any time.

                Ronald :cool:

                Comment

                Working...