Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Javascript] Regular Expression example


  • From: Travis.Falls at thehartford.com (Falls, Travis D (HTSC, CASD))
  • Subject: [Javascript] Regular Expression example
  • Date: Wed Jun 29 13:42:45 2005

I have a regular expression example I am trying to get to work.  I know the
expressions works because I have tested it in the Regular Expression Buddy
application however it does not validate in my JavaScript as true. Does
anyone see what I have done incorrect here?
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <title>Regular Expression Test</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <script language="JavaScript">
  String.prototype.trim=function(){
   return this.replace(/^\s*|\s*$/g,'');
  }
 
  String.prototype.ltrim=function(){
   return this.replace(/^\s*/g,'');
  }
 
  String.prototype.rtrim=function(){
      return this.replace(/\s*$/g,'');
  }
 
 
 
  function isValidTime(value) {
   var returnValue = true;
      var hasMeridian = false;
      var re = /^\d{1,2}[:]\d{2}([:]\d{2})?( [aApP][mM]?)?$/;
      if (!re.test(value)) {
        returnValue = true;
        return returnValue; 
   }
      if (value.toLowerCase().indexOf("p") != -1) { 
    hasMeridian = true; 
   }
      if (value.toLowerCase().indexOf("a") != -1) {
    hasMeridian = true; 
   }
      var values = value.split(":");
      if ((parseFloat(values[0]) < 0) || (parseFloat(values[0]) > 23) ) {
    returnValue = false;
   }
      if (hasMeridian) {
          if ( (parseFloat(values[0]) < 1) || (parseFloat(values[0]) > 12) )
{
     returnValue = false; 
    }
      }
      if ((parseFloat(values[1]) < 0) || (parseFloat(values[1]) > 59) ) {
    returnValue = false;
   }
      if (values.length > 2) {
          if ((parseFloat(values[2]) < 0) || (parseFloat(values[2]) > 59) )
{
     returnValue = false; 
    }
      }
      return returnValue;
  }
  
 
 
 
  function checkPassword (strng) {
   var isvt = new isValidTime();
   var error = "";
   var validTime = new RegExp(/\A([0-1]?[0-9]:[0-5][0-9]([:][0-5][0-9])?([
]?[a,A,p,P][.]?[m,M][.]?)?)\z/);    
   strng = form.time.value.trim();
   if (strng == "") {
     error = "String not filled in";
   }
   else if (validTime.test(strng)) {
          error = "String is formatted correctly.\n" + strng + "\n" +
validTime;
   }
   else if (!validTime.test(strng)) {
    error = "String NOT formatted correctly.\n" + strng + "\n" + validTime;
   }
   alert(error);
   var ivt = new isValidTime(strng);
   if(ivt.returnValue){
    alert("valid");
   }
  }
  
  
 
  </script>
 </head>
<body>
 <form name="form" id="form" onsubmit="return checkPassword();">
  <input id="time" name="time" maxlength="50" type="text" />
  <input type="submit" id="submit" name="submit" />
 </form>
</body>
</html>

Travis D. Falls | Consultant   RAFT.Net   IT | 860.547.4070 |
travis.falls@xxxxxxxxxxxxxxx 





*************************************************************************
PRIVILEGED AND CONFIDENTIAL: This communication, including attachments, is
for the exclusive use of addressee and may contain proprietary,
confidential and/or privileged information.  If you are not the intended
recipient, any use, copying, disclosure, dissemination or distribution is
strictly prohibited.  If you are not the intended recipient, please notify
the sender immediately by return e-mail, delete this communication and
destroy all copies.
*************************************************************************

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.LaTech.edu/pipermail/javascript/attachments/20050629/fcb26ab3/attachment.html