Solved

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

Posted on 2010-11-26
7
747 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Accepted Solution

by:
Aaron Tomosky earned 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Accordion won't close correctly 5 14
Session on Html 8 42
15 puzzle in Javascript/html 2 45
ExcelWebAddIn Connect to SQL database (Javascript or VB.net) 7 29
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

730 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