开发者

String Time Validations (In javascript)

I have a text box which accepts time(max of 5 characters only), and a drop down which accepts am or pm value.

I need to perform some validations for the string values entered into the text box such as:

  1. If user enters 9 => Should be changed to 0900
  2. 9:3 => 0930
  3. 09:3 => 0930
  4. 93 =开发者_开发问答> alert ('Invalid Hours value)
  5. 115 => 0115
  6. 12 => 1200
  7. Invalid entries such as !@#$%^&*()<>?/~`,;'"[]_-abcdefg.. => alert ('Invalid Time Value') should be displayed.

So far, all I've achieved is replacing the : with ''. For example, if user enters 09:00 => 0900

I need something like:

if clks is 1 digit, then rightpad clks with 2 zeroes.

if 2 digits: clks > 12 , then alert(Invalid hours value)

if 3 digits: clks < (%59) (i.e checking last 2 chars) , then leftpad with 1 zero or clks > (%59) , then alert ('Invalid minutes value)

if 4 digits: clks>12% (checking first 2 chars), alert ('invalid hours value') or (clks>12% and clks>59%) , then alert('invalid clock time') or (clks<12%) and (clks<59%) , then accept the number as it is.

if 5 digits: (and all are numbers), then alert('invalid clk time')

These validations need to be done within script tag. (The backend language that I've used is jsp.)

Pls help me :(

Here is a part of the code that I have written:

<script type='text/javascript'>
function clocks(){

    var clk = document.getElementById('TIME').value;
    var clks = clk.replace('/:/g','');
    var ampm = document.getElementById('AMPM').value;
    var add = 1200;

    if (clks=="")
    {
        alert("You must enter clock time");
    }
    else
    {
        if (ampm=='p')
        {
            clks=parseFloat(clks) + parseFloat(add);
        }
    }

}
....
</script>


Here's a way to do it in a function:

function validate_time( clks ) {

   // Remove any non-digit characters
   clks = clks.toString().replace(/\D/g, '');
   var is_valid = false;

   switch (clks.length) {

       case 1:

           // This will run if the length is 1 digit.
           clks = clks + '00';

           // Mark this as a valid time.
           is_valid = true;

           // stop running the rest
           break;

       case 2:

           if ( parseInt(clks) > 12 ) { 
               alert("Invalid hours value"); 
           } else {
               is_valid = true;
           }

           break;

       case 3:

           // Get last two characters
           var mins = clks.substr(1,2);    // offset 1 character, length of 2 characters

           if ( parseInt(mins) <= 59 ) { 
               clks = '0' + clks; 
               is_valid = true;
           } else { 
               alert('Invalid minutes value'); 
           }

           break;

       case 4:

           var hours = clks.substr(0,2);
           var mins = clks.substr(2,2);

           if ( parseInt(hours) > 12 || parseInt(mins) > 59 ) { 
               alert('Invalid clock time'); 
           } else {
               is_valid = true;
           }

           break;

       case 5:

           alert("Invalid clock time");
           break;

    }

    var data = { clks: clks, is_valid: is_valid };

    return data;

}

To call it, you'd do:

var result = validate_time(clks);

and your result would be an object passed back... result.clks is the padded time, and result.is_valid will either be a true or false value as to whether the input time is valid or not.


    function validateTimeNew(obj) {

        var timeValue = obj.value;
        if (timeValue == "" || timeValue.indexOf(":") < 0) {
            alert("Invalid Time format.Valid Format Example  01:56:00 or 23:06:00");
            return false;
        }
        else {
            var sHrs = timeValue.split(':')[0];
            var smin = timeValue.split(':')[1];
            smin = smin.substring(0, 2);
            var sAmnPm = timeValue.split(' ')[1] || timeValue.split(':')[2];
            if (!isNaN(sAmnPm))
            {
                sAmnPm = sAmnPm.toUpperCase();
            }
            var chkAmnPm =timeValue.split(':')[1];             
            if (chkAmnPm.length == 4)
                sAmnPm = chkAmnPm.substring(2, 4);
                 sAmnPm = sAmnPm.toUpperCase();              
            if (sHrs == "" || isNaN(sHrs) || parseInt(sHrs) > 23) {
                alert("Invalid Time format Hours : "+ sHrs);
                return false;
            }
            else if (parseInt(sHrs) == 0) {
                sHrs = "00";
                sAmnPm = "AM";
            }
            else if (sHrs < 10 )
            {
                sHrs = "0" + parseInt(sHrs);
                if (sAmnPm != "PM")
                sAmnPm = "AM";
            }
            else if (sHrs > 13) {
                sHrs = parseInt(sHrs) - 12;
                if (sHrs < 10)
                    sHrs = "0" + parseInt(sHrs);
                    sAmnPm = "PM";
            }
            else if (sHrs == 10 )
            {
                sHrs = parseInt(sHrs);                  
                if (sAmnPm != "PM")
                sAmnPm = "AM";                                     
            }
            if (smin == "" || isNaN(smin) || parseInt(smin) > 59) {
                alert("Invalid Time format Minutes :" + smin);
                return false;
            }
            else if (parseInt(smin) == 0)
                smin = "00";
            else if (smin < 10)
                smin = "0" + parseInt(smin);
            if (sAmnPm == "" || sAmnPm=="undefined")
            {
                sAmnPm = "AM"
            }
            else { sAmnPm = sAmnPm.toUpperCase(); }
            obj.value = sHrs + ":" + smin + ":" + sAmnPm;
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜