http://www.regular-expressions.info/javascriptexample.html

Regulare expressions in java script

>> Friday, March 17, 2006 4:46:36 PM - Comments by satya

Reference page

standard regular expression patterns for numbers

Search for: standard regular expression patterns for numbers

from wiki

regular expression pattern library for common types

Search for: regular expression pattern library for common types


//************************************************
// An internal function used by regexp support
//************************************************
function createFieldType(fieldTypeName, regExp, fieldMessage )
{
   this.fieldTypeName = fieldTypeName;
   this.regExp = new RegExp(regExp);
   this.fieldMessage = fieldMessage;
   return this;
}

function createTypeArray()
{
   typeArray = new Array();
   typeArray.arzCode = new createFieldType("arzCode","^\\w{6}$","ARZ Code is alphanumeric and 6 characters wide");
   typeArray.scac = new createFieldType("scac","^\\w{0,4}$","ARZ Code is alphanumeric and 6 characters wide");
   return typeArray;
}

function validateField(fieldObj, inFieldType)
{
   fieldValue = fieldObj.value;
   if (fieldValue == "") return new Boolean(true);

   var fieldType  = typeArray[inFieldType];
   if (fieldType.regExp.test(fieldValue) == false)
   {
      line1 = "You have typed : '" + fieldValue + "' for " + inFieldType;
      line2 = fieldType.fieldMessage;
      alert(line1 + "\n" + line2);
      fieldObj.focus();
      return new Boolean(false);
   }
   return new Boolean(true);
}

//************************************************
// Validate field content based on pre-defined fieldtypes.
//
// inputs:    form object
//      comma separated fieldNameAndType strings
// returns: true/false
// ex:  validateForContent(thisForm,"field1|field1Type,field2|field2Type")
//************************************************
function validateForContent(form,fieldNames)
{
   if (fieldNames == "")
   {
      return true;
   }
   fieldNameArray = fieldNames.split(",");
   for(i=0;i<fieldNameArray.length;i++)
   {
      fieldNameAndType = fieldNameArray[i];

   // Get field name and type
   fieldNameAndTypeArray = fieldNameAndType.split("|");
   fieldName = fieldNameAndTypeArray[0];
   fieldType = fieldNameAndTypeArray[1];

   // Get field object & value
      fieldObj = eval("form." + fieldName );
   
   if (validateField(fieldObj,fieldType) == false)
   {
      return false;
   }
   }
   return true;
}

This is a better reference


^[-]?\d+\.\d+$

^[-]?\d+$

Use the carot (^) and the end symbol $ to delimit your pattern.


^[-]?\d+\/\d+$

//integer format good:
123, 3, 5, 0023, 2300, -123, -5

//integer format bad:
2.0 0.2 -.3 34. 2.00 2.

//decimal format good:
2.0 2.5 0.345 -0.256

//decimal format bad:
2..1, .3, -.3, 234, 2/5, -2/3, 

//fraction format good
2/5, -2/5, 232/550

//fraction format bad
2//5, /5, /.5, 2.5/2.3, 2.5/3

function testTextAnswerFormat(answer)
{
    //0.23, -0.23
    //won't allow -.23 or .23 or 0..23
    var floatFormat = new RegExp("^[-]?\d+\.\d+$");
    
    //23, -1, 000, 023
    //wont allow decimals: 0.23 
    var intFormat = new RegExp("^[-]?\d+");
    
    //Example: 8/12, -8/12
    //wont allow: hast to have the / char
    //won't allow: floats, ints or 8//9 etc.
    var fractionFormat = new RegExp("^[-]?\d+\/\d+$");
    
    if (intFormat.test(answer) == true)
    {
        //this is an int
        return true;
    }
    if (floatFormat.test(answer) == true)
    {
        //this is a float
        //You can further validate things like 0000.0000
        return true;
    }
    if (fractionFormat.test(answer) == true)
    {
        //this is a fraction: 8/12
        //you can further validate things like 008/0 or 008/0045
        return true;
    }
    return false;
}

where you can avoid the newing of regex objects multiple times.

to further evaluate the portions of the number.

Here is a list of examples and standard formats

Another nice intro from Mozilla


var floatFormat = new RegExp("^[-]?\\d+\\.\\d+$");

//when all I needed was

^[-]?\d+\.\d+$

var intFormat = /^[-]?\d+$/;

//instead of 

var intFormat = new RegExp("^[-]?\\d+$");
//Note the double backslashes


function testTextAnswerFormat(answer)
{
    //0.23, -0.23
    //won't allow -.23 or .23 or 0..23
    //var floatFormat = new RegExp("^[-]?\\d+\\.\\d+$");
    var floatFormat = /^[-]?\d+\.\d+$/;
    
    //23, -1, 000, 023
    //wont allow decimals: 0.23 
    //var intFormat = new RegExp("^[-]?\\d+$");
    var intFormat = /^[-]?\d+$/;
    
    //Example: 8/12, -8/12
    //wont allow: hast to have the / char
    //won't allow: floats, ints or 8//9 etc.
    //var fractionFormat = new RegExp("^[-]?\\d+\\/\\d+$");
    var fractionFormat = /^[-]?\d+\/\d+$/;
    
    alert("Testing value:" + answer);
    if (intFormat.test(answer))
    {
        //this is an int
        alert("int");
        return true;
    }
    if (floatFormat.test(answer) == true)
    {
        //this is a float
        //You can further validate things like 0000.0000
        alert("float");
        return true;
    }
    if (fractionFormat.test(answer) == true)
    {
        //this is a fraction: 8/12
        //you can further validate things like 008/0 or 008/0045
        alert("fraction");
        return true;
    }
    return false;
}