Thursday, June 2, 2011

Validation functions in JavaScript for Date and Password

<script type="text/javascript" language="javascript">function ValidateDate(inval) {

//var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;//var re = /(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/;
var message = "OK!";
var Day, Mn, Yr;
var re = /^(\d{1,2})[-.\/](\d{1,2})[-.\/](\d{4})$/;
if (!inval.match(re))
message = "Enter Date in Valid Format";

re.exec(inval);
Day = RegExp.$1;
Mn = RegExp.$2;
Yr = RegExp.$3;
var DateVal = Mn + "/" + Day + "/" + Yr;
var dt = new Date(DateVal);
if (dt.getDate() != Day) {
message = "Invalid Day!"}
else if (dt.getMonth() != Mn - 1) {
//this is for the purpose JavaScript starts the month from 0message = "Invalid Month!"}
else if (dt.getFullYear() != Yr) {
message = "Invalid Year!"}
alert(message);

}
function ValidatePassword(inval) {
var message = "";
//alert(inval);var re = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{6,}$/;
//alert(re.test(inval));re = /(?=.*\d)/;
message += (re.test(inval) == false) ? "Please enter at least one numeric\n" : "";
re = /(?=.*[a-z])/;
message += (re.test(inval) == false) ? "Please enter at least one small char\n" : "";
re = /(?=.*[A-Z])/;
message += (re.test(inval) == false) ? "Please enter at least one cap char\n" : "";
re = /\w{6,}/;
message += (re.test(inval) == false) ? "Length should be more than 6 chars\n" : "";
if (message != "")
alert(message);
}
</script>



===================================================

<script type="text/javascript" language="javascript">
        function ValidateDate(inval) {
       
            //var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
            //var re = /(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/;
       
            var message = "OK!";
            var Day, Mn, Yr;
            var re = /^(\d{1,2})[-.\/](\d{1,2})[-.\/](\d{4})$/;
            if (!inval.match(re))
                message = "Enter Date in Valid Format";
           
            re.exec(inval);
            Day = RegExp.$1;
            Mn = RegExp.$2;
            Yr = RegExp.$3;
            var DateVal = Mn + "/" + Day + "/" + Yr;
            var dt = new Date(DateVal);
            if (dt.getDate() != Day) {
                message = "Invalid Day!"
            }
            else if (dt.getMonth() != Mn - 1) {
                //this is for the purpose JavaScript starts the month from 0
            message = "Invalid Month!"
            }
            else if (dt.getFullYear() != Yr) {
            message = "Invalid Year!"
            }
            alert(message);
           
        }
        function ValidatePassword(inval) {
            var message = "";
            //alert(inval);
            var re = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{6,}$/;
            //alert(re.test(inval));
            re = /(?=.*\d)/;
            message += (re.test(inval) == false) ? "Please enter at least one numeric\n" : "";
            re = /(?=.*[a-z])/;
            message += (re.test(inval) == false) ? "Please enter at least one small char\n" : "";
            re = /(?=.*[A-Z])/;
            message += (re.test(inval) == false) ? "Please enter at least one cap char\n" : "";
            re = /\w{6,}/;
            message += (re.test(inval) == false) ? "Length should be more than 6 chars\n" : "";
            if (message != "")
                alert(message);
        }
    </script>

No comments:

Post a Comment