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: shawn.milo at gmail.com (Shawn Milo)
  • Subject: [Javascript] Regular Expression example
  • Date: Wed Jun 29 14:12:08 2005

Travis,

Delighted to help. Several notes:

1. It looks like you're a VBScript coder, as am I. The syntax in
JavaScript is .match, not .test. ;-)

2. I fixed the regex to make it a little simpler and a little more
comprehensive.
   It allowed  0:00 but not 13:00, so I figured you didn't want military time.

3. You were referring to the text box in a non-standard way, so I fixed it.

4. You had an argument in the function declaration, but you weren't
passing anything to the function. I took it out. I'm sure you're
planning to change it to accept one, but it's "correct" for now.

5. I would maybe clean up that three-part if with an outer if, then
make the match pass/fail a nested if/else. Just my preference -- I
think it looks cleaner.

Tested and working.  :o)


Shawn   


<!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 (u) {
   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/);
   //var validTime = new
RegExp(/([0-1]?[0-9]:[0-5][0-9]([:][0-5][0-9])?(\s*[aApP]\.?[mM]?\.?)?)/);
   var validTime = new
RegExp(/^(1[0-2]|[1-9]):[0-5][0-9](:[0-5][0-9])?(\s+[aApP]\.?[mM]\.?)?$/);
   strng = document.forms['form'].time.value;
   strng = strng.replace(/^\s*(.*)\s*$/, '$1')

   if (strng == "") {
     error = "String not filled in";
   }
   else if (strng.match(validTime)) {
          error = "String is formatted correctly.\n" + strng + "\n" + validTime;
   }
   else if (!strng.match(validTime)) {
    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>









On 6/29/05, Falls, Travis D (HTSC, CASD) <Travis.Falls@xxxxxxxxxxxxxxx> wrote:
>  
> 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.
> *************************************************************************
>  
> _______________________________________________
> Javascript mailing list
> Javascript@xxxxxxxxxx
> https://lists.LaTech.edu/mailman/listinfo/javascript
> 
> 
> 


-- 
Shawn Milochik

The Freelance Pen
FreelancePen.com
610-621-2648