Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

regex for special HEX value

Last post 08-29-2008, 10:15 AM by prometheuzz. 3 replies.
Sort Posts: Previous Next
  •  08-29-2008, 9:40 AM 45774

    regex for special HEX value

    Hi,

    first of all: i have absolutely no experience with regular expressions, and just a little bit with java script. :-|

    The Task: I have a HTML form (name "Sensors") where a special sensor ID has to be entered : <INPUT type="text" size="40" maxlength="16" name="Sensor1_ID" value=""/>

    I need to check the content of the text field with following rules:

    - The Sensor-ID must have 16 characters

    - Only HEX values are allowed (0-9 a-f A-F)

    - The last two characters (position 15+16) must be "10" or "28".

    What i currently have:

    Following JavaScript checks (OnSumbit) the content of the text field:

    function CheckSensor () {
        if (document.Sensors.Sensor1_ID.value.length != 16) {
          alert("Invalid ID!")
          document.Sensors.Sensor1_ID.focus();
          return false;
        }
    }

    What i need:

    Add a regular expression for condition 2 and 3.

     Thank you in advance!

  •  08-29-2008, 9:50 AM 45776 in reply to 45774

    Re: regex for special HEX value

    Try this: 

    if(document.Sensors.Sensor1_ID.value.match(/^[a-f\d]{14}(?:10|28)$/i)) {
      // valid
    } else {
      // invalid
    }

     

    A (short) explanation: 

    ^                 -   the beginning of the string
    [a-f\d]{14}   -   followed by 14 numerical values or the characters 'a', 'b', 'c', 'd', 'e' or 'f'
    (?:10|28)     -   followed by either '10' or '28'
                    -   followed by the end of the string

  •  08-29-2008, 10:11 AM 45777 in reply to 45776

    Re: regex for special HEX value

    Perfect ! Thank you !!!
  •  08-29-2008, 10:15 AM 45778 in reply to 45777

    Re: regex for special HEX value

    You're welcome!
View as RSS news feed in XML