?
Solved

Mootools: How to validate time on input field in form?

Posted on 2010-11-26
7
Medium Priority
?
751 Views
Last Modified: 2012-06-21
I have a form that takes times in text field inputs in 24-hour format.  

I want to be able to validate the contents of these inputs to ensure that they are usable i.e. from 00:00-23:59 and no different.

The page uses the Mootools framework, and so I need to be able to deploy this validation using Mootools - I've taken a look at the default Form.Validator (http://mootools.net/docs/more/Forms/Form.Validator) but this has date format validation but nothing to do with time (I need to check the ':' is used too.)

Thanks
0
Comment
Question by:freestate
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 2

Expert Comment

by:marceloOrigoni
ID: 34217430
This is a code I wrote some time ago:
                input = $('[idOfYourInput']);
            input.setProperty('maxlength', 5);
            input.addEvent('keydown', function(event){
                  length = this.getProperty('value').length;
                  return isNumberKey(event, length);
                  });
            input.addEvent('keyup', function(event){
                  code = event.code;
                  key = event.key;
                  if((code > 47 && code < 58) || (code > 95 && code < 106)){
                        limit = 23;
                        
                        input.setStyle('color', '#444444');
                        length = this.getProperty('value').length;
                                                
                        if(length != 2 && length != 5){
                              hoursError = false;
                              }
                              
                        if(length == 2){
                              text = this.getProperty('value');
                              if(text.toInt() > limit){
                                    input.setStyle('color', '#ff0000');
                                    hoursError = true;
                              }else{
                                    hoursError = false;
                              }
                        
                              if(hoursError == false){
                                    this.setProperty('value',text + ":");
                              }
                        }
                        if(length == 5){
                              hs = this.getProperty('value').split(":")[0];
                              min = this.getProperty('value').split(":")[1];
                              if(hs.toInt() > limit || hs.toInt() < 0){
                                    input.setStyle('color', '#ff0000');
                                    hoursError = true;
                              }else{
                                    hoursError = false;
                              }
                              if(min.toInt() > 59 || min.toInt() < 0){
                                    input.setStyle('color', '#ff0000');
                                    hoursError = true;
                              }else{
                                    if(!hoursError){
                                          hoursError = false;
                                    }
                              }
                        }
                  }
            });
            
function isNumberKey(event, length) {
        var code = event.code;
            if(code == 8 || code == 9 || code == 37 || code == 39 ){
                  return true;            
            }
        if ((code > 47 && code < 58) || (code > 95 && code < 106)){
                  if(length != 2 && length != 5){
                        return true;
                  }
                  else{
                        if(hoursError){
                              return false;
                        }
                        else{
                              return true;
                        }
                  }
            }
            else{
                  return false;
            }
    }      

 It will validate the format, while you are typing, and on submit, you can check the hoursError variable, to see if there is any error.

 Marcelo
0
 

Author Comment

by:freestate
ID: 34217458
Hi,

Sorry - could you give a simple example of how to deploy this? i.e. I have:

<input type="text" name="reception_time" id="reception_time" /> as one of the fields that needs checking...

Thanks!
0
 
LVL 2

Expert Comment

by:marceloOrigoni
ID: 34217539
I made something quickly:

<html>
<head>
            <script type="text/javascript" src="./js/mootools.yc.js"></script>
<script type="text/javascript">
            var hoursError = true;
            window.addEvent('domready', function(){
                  input = $('reception_time');
                  form = $('form');
                  form.addEvent('submit', function(e){
                        if(hoursError){
                              alert('Time Format should be HH:MM');
                              return false;
                        }else{
                              return true;
                        }
                  });
            input.setProperty('maxlength', 5);
            input.addEvent('keydown', function(event){
                  length = this.getProperty('value').length;
                  return isNumberKey(event, length);
                  });
            input.addEvent('keyup', function(event){
                  code = event.code;
                  key = event.key;
                  if((code > 47 && code < 58) || (code > 95 && code < 106)){
                        limit = 23;
                       
                        input.setStyle('color', '#444444');
                        length = this.getProperty('value').length;
                                               
                        if(length != 2 && length != 5){
                              hoursError = false;
                              }
                             
                        if(length == 2){
                              text = this.getProperty('value');
                              if(text.toInt() > limit){
                                    input.setStyle('color', '#ff0000');
                                    hoursError = true;
                              }else{
                                    hoursError = false;
                              }
                       
                              if(hoursError == false){
                                    this.setProperty('value',text + ":");
                              }
                        }
                        if(length == 5){
                              hs = this.getProperty('value').split(":")[0];
                              min = this.getProperty('value').split(":")[1];
                              if(hs.toInt() > limit || hs.toInt() < 0){
                                    input.setStyle('color', '#ff0000');
                                    hoursError = true;
                              }else{
                                    hoursError = false;
                              }
                              if(min.toInt() > 59 || min.toInt() < 0){
                                    input.setStyle('color', '#ff0000');
                                    hoursError = true;
                              }else{
                                    if(!hoursError){
                                          hoursError = false;
                                    }
                              }
                        }
                  }
            });
                  
            });
function isNumberKey(event, length) {
        var code = event.code;
            if(code == 8 || code == 9 || code == 37 || code == 39 ){
                  return true;            
            }
        if ((code > 47 && code < 58) || (code > 95 && code < 106)){
                  if(length != 2 && length != 5){
                        return true;
                  }
                  else{
                        if(hoursError){
                              return false;
                        }
                        else{
                              return true;
                        }
                  }
            }
            else{
                  return false;
            }
    }      
</script>
            </head>
<body>            
      <form id="form" method="GET" action="form.html" >
            <input type="text" name="reception_time" id="reception_time" />
            <input type="submit" name="submit" id="submit" value="Go!" />
      </form>
</body>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 39

Accepted Solution

by:
Aaron Tomosky earned 2000 total points
ID: 34219147
A regex is much simpler and faster.
http://mobile.experts-exchange.com/Q_22953566.html
0
 

Author Comment

by:freestate
ID: 34236990
Good point...

Had to google a 24 hour clock regex, so here's the modified version of the code on that page for anyone who needs it:

0
 

Author Comment

by:freestate
ID: 34237003
<html>
<head>
<title>Time Check</title>
<script language = "javascript">
<!--
function validateDate(){
   var RegEx = /^([0-1]?[0-9]|2[0-4]):([0-5][0-9])(:[0-5][0-9])?$/;

   if(!document.form1.fieldToValidate.value.match(RegEx)){
      alert("Invalid time.");
      return false;
   }
   alert ("OK!");
   return true;
}
-->
</script>
</head>
<body>
<form name="form1">
<input type = "text" name = "fieldToValidate" value = "">
<input type = "button" value = "Validate" onclick = "validateDate()">
</form
</body>

Open in new window

0
 
LVL 2

Expert Comment

by:marceloOrigoni
ID: 34237738
That code is wrong, it will allow to input number between 24:00 and 24:59

this is thw corrent regex
 var RegEx = /^([0-1]?[0-9]|2[0-3]):([0-5][0-9])(:[0-5][0-9])?$/;
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question