Search these areas:
Help
Support Forums
Product Info



-Introduction
-DataWeb: What's New
-Creating an Application
-The DataWeb Designer
-Application Management
-Data Tables
-Data Tables
-Views
-Templates
-Components
-Data Analysis
-Members
-Security
-Importing
-Exporting
-FTP
-JavaScript
-Object Model
-Full-Text Search
-Domain and Email Hosting
-HotBlock Modules
-Account Management
-Glossary
-New Responsive UI Module





DataWeb Help
Support Forums
Tutorial
Script Library
Knowledge Base

Home > Support

Script Library

  Phone Number Data Validation
 
  chubbard posted this script:
  This script validates input of a US (or Canada) phone number.  Also, included is a BeforeUpdate function for a table "Contacts" which contains home and work phone fields.  For a real table, you would also want to add this validation to the BeforeInsert handler.

function Data_BeforeUpdate(record, previous)
    {
    ValidatePhone("Contacts", "PhoneHome", record);
    ValidatePhone("Contacts", "PhoneWork", record);

    if (Response.hasFieldErrors())
        {
        Response.SetReshow();
        return false;
        }  
    }

private function ValidatePhone(strTable, strCol, record, required)
    {
    if (arguments.length < 4)
        required = false;

    // Handles the following format
    //    (1) 1[+-]<number>
    //    (2) <number> = [(]###[)][-]###[-]####
    //    (3) white space is allowed anywhere

    var strPhone = record[strCol];
    if (strPhone)
        strPhone = strPhone.trim();
    if (!required && !strPhone)
        {
        return;
        }
    else if (!strPhone || strPhone.length < 10)
        {
        Response.addFieldError(strTable, strCol, "Please enter a valid United States phone number, including area code.");
        }
    else
        {
        // Remove all white space.
        var arr = strPhone.split(' ');
        strPhone = arr.join('');

        // Remove other valid formatting.
        if (strPhone.substring(0, 2) == "1+" || strPhone.substring(0, 2) == "1-")
            strPhone = strPhone.substring(2, strPhone.length);
        if (strPhone.charAt(0) == "(" && strPhone.charAt(4) == ")")
            strPhone = strPhone.substring(1, 4) + strPhone.substring(5, strPhone.length);
        if (strPhone.charAt(3) == "-")
            strPhone = strPhone.substring(0, 3) + strPhone.substring(4, strPhone.length);
        if (strPhone.charAt(6) == "-")
            strPhone = strPhone.substring(0, 6) + strPhone.substring(7, strPhone.length);

        if (strPhone.length < 10)
            Response.addFieldError(strTable, strCol, "The phone number you entered does not contain enough digits.  Please be sure to include the area code.");
        else
            {
            // Check for numeric digits.
            var num;
            for (var i = 0; i < strPhone.length; i++)
                {
                if ((num = Number(strPhone.charAt(i))) == undefined)
                    break;
                }
            if (num == undefined)
                Response.addFieldError(strTable, strCol, "Invalid formatting character '" + strPhone.charAt(i) + "' found.  Please enter a valid phone number.");
            else if (strPhone.length > 10)
                Response.addFieldError(strTable, strCol, "This phone number contains too many digits.  Please enter a phone number within the United States or Canada.");
            else
                {
                // Okay, valid phone number. Now set to standard format.
                record[strCol] = "(" + strPhone.substring(0, 3) + ") " + strPhone.substring(3, 6) + "-" + strPhone.substring(6, 10);
                }
            }
        }
    }
 
Affiliate | Partner | Terms of Use | Privacy Policy | Contact Us | Pricing | Bring DataWeb In-House    
DataWeb, 720 North 10th Street, A #145, Renton, Washington 98057 *425-583-5970* Fax 484-770-4706* Email Us