﻿/* 

 * Classs Name      : Validation's Scipts.(JSValidations.js)
 * Created By       : Anuprita Ingale
 * Date             : 07 July 2011
 * Description      : Collection of Common Validations functions.
 * Dependent        : JSConstants.js
 --------------------------------------------------------------------------------------------------------
          Change History (Source Safe)
---------------------------------------------------------------------------------------------------------
   Date:			Author:                 Description:
---------------------------------------------------------------------------------------------------------

07 July 2011     Anuprita Ingale         Created;
---------------------------------------------------------------------------------------------------------
Added functions->
function  CheckAgainstRegX(regMy,strText)

function AlphaNum(strText)//AlphaNum is allowed.Only digits are allowed.
function AlphaNum_NoNum(strText)//AlphaNum is allowed.Only alphabets/digits are not  allowed
function AlphaNum_Space_NoNum(strText)//AlphaNum with space is allowed.Only digits are not allowed.
function AlphaNum_Dash_NoNum(strText)//AlphaNum with only Dash(-) is allowed.Only digits are not allowed.
function AlphaNum_Space_Dash_NoNum(strText)//AlphaNum with  Dash(-) & space allowed.Only digits are not allowed.
function AlphaNum_Space_Dash_Dot_NoNum(strText)//AlphaNum with  Dash(-),Dot(.) & Space  allowed.Only digits are not allowed.
function AlphaNum_Space_Num(strText)//AlphaNum with  Space  allowed.Only digits are allowed.
function Alphabets(strText){return //alphabets allowed.
function Alphabets_Space(strText)//alphabets with  Space  allowed.
function Alphabets_Space_Dash(strText)//alphabets with  Space,Dash(-) allowed.
function Alphabets_Space_Dash_Dot(strText)//alphabets with  Space,Dash(-),Dot allowed.

function ValidateInt(strText)//allow int greater than or equal to zero.
function ValidateIntNonZero(strText)//allow int greater than zero.
function ValidateDecimal(strText)//allow float greater than or equal to zero.
function ValidateDecimalNonZero(strText)//allow float greater than zero.

function ValidateEmail(strText)//Validation for Email id
function ValidateMobile(strText)//Validation for mobile Number,allows '+'
function ValidatePhone(strText)//Validation for landline Number,allows '+'
function ValidateWebsite(strText)//Validation for website 
function ValidateCountryCode(strText)//Validation for Country Code 
function ValidatePinCode(strText)//Validation for Pin Code 
function ValidateDate_DMY(strText)//Validation for Date
function ValidateDate_MDY(strText)//Validation for Date
function ValidateDate_YMD(strText)//Validation for Date
 ---------------------------------------------------------------------------------------------------------

 ---------------------------------------------------------------------------------------------------------
*/

//regular expression constants
//#region
var regAlphaNum = /^[a-zA-Z0-9]+$/;
var regAlphaNum_NoNum = /(^[a-zA-Z]+[a-zA-Z0-9]*$|^[a-zA-Z0-9]+[a-zA-Z]+$)/;
var regAlphaNum_Space_NoNum = /^[a-zA-Z]+[0-9]*(( )?[a-zA-Z0-9]+)*$/;
var regAlphaNum_Dash_NoNum = /^[a-zA-Z]+[0-9]*((-)?[a-zA-Z0-9]+)*$/;
var regAlphaNum_Space_Dash_NoNum = /^[a-zA-Z]+[0-9]*((-)?( )?[a-zA-Z0-9]+)*$/;
var regAlphaNum_Space_Dash_Dot_NoNum=/^[a-zA-Z]+[0-9]*((-)?( )?(.)?[a-zA-Z0-9]+)*$/;
var regAlphaNum_Space_Num=/^[a-zA-Z0-9]+(( )?[a-zA-Z])+$/;
var regAlphabets_Space=/^[a-zA-Z]+((\s)?[a-zA-Z]+)*$/;
var regAlphabets_Space_Dash=/^[a-zA-Z]+(( |-)?[a-zA-Z]+)*$/;
var regAlphabets_Space_Dash_Dot=/^[a-zA-Z]+(([ ]?[-]?[.]?)[a-zA-Z.]+)*$/;
var regAlphabets=/^[a-zA-Z]+$/;

var regValidateInt=/^\s*\d+\s*$/;
var regValidateIntNonZero=/^[1-9]+[0-9]*$/;
var regValidateDecimal=/^(\d)+(\.)?(\d)*$/;
var regValidateDecimalNonZero=/^(\d)+(\.)?(\d)*$/;

var regEmail=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
var regMobile=/^[+]?[0-9]*$/;
var regLandline=/^[0-9]*[-]?[0-9]*$/;
var regWebsite=/((www.){1})(([A-Za-z0-9\-_]{3,}).)((com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk){1})/;
var regHttpURL=/^http\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?$/;
var regCountryCode=/^[0-9]*$/;
var regPinCode=/^[0-9]*$/;
var regDate_DMY=/^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;//dd-mm-yyyy format
var regDate_MDY=/^([1-9]|0[1-9]|1[012])[- \\ \/.]([1-9]|0[1-9]|[12][0-9]|3[01])[- \\ \/.][0-9]{4}$/;//mm-dd-yyyy format
var regDate_YMD=/^[0-9]{4}[- \/.]([1-9]|0[1-9]|[12][0-9]|3[01])[- \/.]([1-9]|0[1-9]|1[012])$/;//yyyy-mm-dd format
//#endregion
 

//validations
//#region

function  CheckAgainstRegX(regMy,strText)
{
 if(regMy.test(strText.toString()))
         {
             return RETURN_SUCCESS  ;
         }      

    return RETURN_FAIL;
}//End of return CheckAgainstRegX


//Functions for AlphaNum
//#region 

function AlphaNum(strText){ return CheckAgainstRegX(regAlphaNum,strText);};//AlphaNum is allowed.Only digits are allowed.
function AlphaNum_NoNum(strText){ return CheckAgainstRegX(regAlphaNum_NoNum,strText);};//AlphaNum is allowed.Only alphabets/digits are not  allowed
function AlphaNum_Space_NoNum(strText){return CheckAgainstRegX(regAlphaNum_Space_NoNum,strText);};//AlphaNum with space is allowed.Only digits are not allowed.
function AlphaNum_Dash_NoNum(strText){return CheckAgainstRegX(regAlphaNum_Dash_NoNum,strText);};//AlphaNum with only Dash(-) is allowed.Only digits are not allowed.
function AlphaNum_Space_Dash_NoNum(strText){return CheckAgainstRegX(regAlphaNum_Space_Dash_NoNum,strText);};//AlphaNum with  Dash(-) & space allowed.Only digits are not allowed.
function AlphaNum_Space_Dash_Dot_NoNum(strText){return CheckAgainstRegX(regAlphaNum_Space_Dash_Dot_NoNum,strText);};//AlphaNum with  Dash(-),Dot(.) & Space  allowed.Only digits are not allowed.
function AlphaNum_Space_Num(strText){return CheckAgainstRegX(regAlphaNum_Space_Num,strText);};//AlphaNum with  Space  allowed.Only digits are allowed.
function Alphabets(strText){return CheckAgainstRegX(regAlphabets,strText);};//alphabets allowed.
function Alphabets_Space(strText){return CheckAgainstRegX(regAlphabets_Space,strText);};//alphabets with  Space  allowed.
function Alphabets_Space_Dash(strText){return CheckAgainstRegX(regAlphabets_Space_Dash,strText);};//alphabets with  Space,Dash(-) allowed.
function Alphabets_Space_Dash_Dot(strText){return CheckAgainstRegX(regAlphabets_Space_Dash_Dot,strText);};//alphabets with  Space,Dash(-),Dot allowed.

//#endregion

//Functions for numbers
//#region 

function ValidateInt(strText){if( CheckAgainstRegX(regValidateInt,strText)){if(parseInt(strText) >= 0){return RETURN_SUCCESS;}return RETURN_FAIL;}};//allow int greater than or equal to zero.
function ValidateIntNonZero(strText){if( CheckAgainstRegX(regValidateIntNonZero,strText)){if(parseInt(strText) > 0){return RETURN_SUCCESS;}return RETURN_FAIL;}};//allow int greater than zero.
function ValidateDecimal(strText){if( CheckAgainstRegX(regValidateDecimal,strText)){if(parseFloat(strText) >= 0){return RETURN_SUCCESS;}return RETURN_FAIL;}};//allow float greater than or equal to zero.
function ValidateDecimalNonZero(strText){if( CheckAgainstRegX(regValidateDecimalNonZero,strText)){if(parseFloat(strText) >= 0){return RETURN_SUCCESS;}return RETURN_FAIL;}};//allow float greater than zero.

//#endregion

//other functions
//#region

function ValidateEmail(strText){return CheckAgainstRegX(regEmail,strText);};//Validation for Email id
function ValidateMobile(strText){return CheckAgainstRegX(regMobile,strText);};//Validation for mobile Number,allows '+'
function ValidatePhone(strText){return CheckAgainstRegX(regLandline,strText);};//Validation for landline Number,allows '+'
function ValidateWebsite(strText){return CheckAgainstRegX(regWebsite,strText);};//Validation for website 
function ValidateHttpUrl(strText){ return CheckAgainstRegX(regHttpURL,strText);};//Validation for website 
function ValidateCountryCode(strText){return CheckAgainstRegX(regCountryCode,strText);};//Validation for Country Code 
function ValidatePinCode(strText){return CheckAgainstRegX(regPinCode,strText);};//Validation for Pin Code 
function ValidateDate_DMY(strText){return CheckAgainstRegX(regDate_DMY,strText);};//Validation for Date
function ValidateDate_MDY(strText){return CheckAgainstRegX(regDate_MDY,strText);};//Validation for Date
function ValidateDate_YMD(strText){return CheckAgainstRegX(regDate_YMD,strText);};//Validation for Date

//#endregion

// Trim function in javascript 
function Trim(str)
{
    while (str.substring(0,1) == ' ') // check for white spaces from beginning
    {
        str = str.substring(1, str.length);
    }
    while (str.substring(str.length-1, str.length) == ' ') // check white space from end
    {
        str = str.substring(0,str.length-1);
    }
   
    return str;
}

//validate temperature (allows + and -)
function  ValidateTemperature(strText)
{
            if(!isNaN(strText))
            {
                return RETURN_SUCCESS;
            }
     return RETURN_FAIL; 
}



//#endregion


