Posted By

jfdesgagne on 09/08/10


Tagged

hex regexp hexadecimal 64bits 128bits


Versions (?)

Hex Validation


 / Published in: ActionScript 3
 

  1. public class HexUtils {
  2. public static const HEX_64_BITS:int = 10;
  3. public static const HEX_128_BITS:int = 26;
  4.  
  5. public static function isValidHex(_val:String, ...args:Array):Boolean {
  6. //splice the 0x if it's there
  7. if(_val.substr(0, 2)=="0x") _val = _val.substr(2);
  8.  
  9. //this part is not tested
  10. var length:Array = [HEX_64_BITS, HEX_128_BITS];
  11. if(args.length>0) {
  12. if(args[0] is Array) {
  13. length = args[0];
  14. } else {
  15. length = [args[0]];
  16. }
  17. }
  18.  
  19. //test with hex char
  20. if(length.indexOf(_val.length)>-1) {
  21. if(_val.match(/^[A-Fa-f0-9]*$/)) {
  22. return true;
  23. }
  24. }
  25.  
  26. return false;
  27. }
  28. }
  29.  
  30. TESTING:
  31.  
  32. var hexToTest:Array = ['0x1122334455', '1122334455', '0x112233445', '0xabcde12345', '0xz123456789', '0xabcdeaaaaa'];
  33. for each(var hex:String in hexToTest) {
  34. trace('isHexValid', hex, HexUtils.isValidHex(hex));
  35. }

Report this snippet  

You need to login to post a comment.