Broad Network


Validation of known and Unknown Data Types

ECMAScript Validation of HTML Form Data Part 1

Web Development with ECMAScript and MySQL in Node.js

Foreword: In this part of the series, I explain how to validate known and unknown HTML Form data types.

By: Chrysanthus Date Published: 5 Sep 2016

Introduction

This is part 1 of my series, ECMAScript Validation of HTML Form Data. In this part of the series, I explain how to validate known and unknown HTML Form data types. In all the code samples below, I assume you have got datum in a variable from the web page Form.

Pre-Knowledge
This series is part of my volume, Web Development with ECMAScript and MySQL in Node.js . At the bottom of the page you have the series in the volume. From there you should know what you should have studied first before coming here.

The Boolean Datum
In ECMAScript, 0, null, undefined and "" are each effectively false; any other thing is true. To test if a variable is effectively true, use the following code:

    variab = "something";
    
    variab = variab.replace(/^\s*|\s*$/g, '');  //remove leading and trailing whitespaces, does not work for undefined or null variable

    function istrue(argue)
        {
            if ((argue == undefined)||(argue == null)||(argue == false)||(argue == "")||(argue == 0))
                return false; //for not true
            else
                return true; //for true
        }

    console.log(istrue(variab));

To know if the variable is Boolean (i.e. either exactly ture or exactly false), use the following code:

    variab = true;
    
    function isBoolean(argue)
        {
            if (argue === false)
                return true; //for not true
            else if (argue === true)
                return true; //for true
            else
                rreturn false;
        }

    console.log(isBoolean(variab));

Number
To know whether a variable is a number, use the following code:

    variab = 56.72;

    ret = Number.isFinite(variab);

    console.log(ret);

If it is a number, the code will return true, otherwise it will return false.

Integer
The following code returns true if the variable is an integer and false if the variable is not.

    variab = 55;

    ret = Number.isInteger(variab);

    console.log(ret);

Email Data Type
The following code will return true for a valid common email and false otherwise:

    variab = "very.common@example.com";
    variab = variab.replace(/^\s*|\s*$/g, '');  //remove leading and trailing whitespaces, does not work for undefined or null variable

    function isEmail(argue)
        {
            temp = false;

            if (argue.search(/^[0-9a-zA-Z_\.-]{1,64}@[0-9a-zA-Z_-]{1,252}(\.[0-9a-zA-Z_\-]{2,4}){0,2}$/) != -1)
                {
                    temp = true;
                }

            if (temp == true)
                {
                    if (argue.length <= 254)
                        {
                            return true;
                        }
                    else
                        {
                            return false;
                        }                                    
                }
            else
                {
                    return false;
                }
        }

    console.log(isEmail(variab));

URL Data Types
The following code will return true for a valid common URL and false otherwise:

    variab = "http://www.broad-network.com/ChrysanthusForcha/Web-Development-Course.htm#PPI";
    variab = variab.replace(/^\s*|\s*$/g, '');  //remove leading and trailing whitespaces, does not work for undefined or null variable

    function isURL(argue)
        {
            if (argue.search(/^http|https:\/\/([0-9a-zA-Z_\-]{1,64}\.)?[0-9a-zA-Z_\-]{1,64}(\.[0-9a-zA-Z_\-]{2,4}){0,2}(:[0-9]{1,5})?(\/[0-9a-zA-Z_\-]{1,64}){0,64}([0-9a-zA-Z_\-]{1,64}(\.[a-zA-Z]{1,4})?)?(#[0-9a-zA-Z_\-]{1,64})?.*/) != -1)
                {
                    return true;
                }
            else
                {
                    return false;
                }
        }

    console.log(isURL(variab));

The IP Data Type
The following code will return 1 for a valid IP address and zero otherwise:

    variab = "172.16.254.45";
    variab = variab.replace(/^\s*|\s*$/g, '');  //remove leading and trailing whitespaces, does not work for undefined or null variable

    function isIP(argue)
        {
            if ((argue.search(/^[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}$/) != -1) || (argue.search(/^[\da-fA-F]{1,4}:[\da-fA-F]{1,4}:[\da-fA-F]{1,4}:[\da-fA-F]{1,4}:[\da-fA-F]{1,4}:[\da-fA-F]{1,4}:[\da-fA-F]{1,4}:[\da-fA-F]{1,4}$/) != -1))
                {
                    return true;
                }
            else
                {
                    return false;
                }
        }

    console.log(isIP(variab));

Regex
You do not really have any data type, like the regex data type. However, if you have to validate any text that does not fall into any of the above category, then use regular expression.

Assume you have the subject,

    variab = "cork";

and the regex, /c.rk/

then the following code will do the validation:

    variab = "cork";

    variab = variab.replace(/^\s*|\s*$/g, '');  //remove leading and trailing whitespaces, does not work for undefined or null variable

    function isRT(argue)
        {
            if (argue.search(/c.rk/) != -1)
                {
                    return true;
                }
            else
                {
                    return false;
                }
        }

        console.log(isRT(variab));

String
The following code returns true if the variable is a string:

    variab = " a stri ";

    variab = variab.replace(/^\s*|\s*$/g, '');  //remove leading and trailing whitespaces, does not work for undefined or null variable

    function isString(argue)
        {
            if (argue.search(/\D/) != -1)
                {
                    return true;
                }
            else
                {
                    return false;
                }
        }

        console.log(isString(variab));

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

Web Development Basics with ECMAScript (JavaScript) and MySQL
ECMAScript (JavaScript) Validation of HTML Form Data
Web Live Text Chart Application using ECMAScript (JavaScript) and MySQL
Page Views with Ajax and ECMAScript (JavaScript) and MySQL
Search Within a Site using ECMAScript and MySQL
More Related Links
Node Mailsend with JavaScript
EMySQL API with JavaScript
Node.js Web Development Course with JavaScript
Major in Website Design
Low Level Programming - Writing ECMAScript (JavaScript) Module
ECMAScript (JavaScript) Course

NEXT

Comments

Become the Writer's Follower
Send the Writer a Message