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

satya - 6/1/2014 8:51:15 PM

standard regular expression patterns for numbers

standard regular expression patterns for numbers

Search for: standard regular expression patterns for numbers

satya - 6/1/2014 8:51:34 PM

from wiki

from wiki

satya - 6/1/2014 8:56:33 PM

regular expression pattern library for common types

regular expression pattern library for common types

Search for: regular expression pattern library for common types

satya - 6/1/2014 9:02:29 PM

Here is what I seem to have done before. some sample code


//************************************************
// 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;
}

satya - 6/1/2014 9:08:40 PM

This is a better reference

This is a better reference

satya - 6/1/2014 9:17:26 PM

A float that has to have a decimal point


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

satya - 6/1/2014 9:20:21 PM

An integer positive or negative


^[-]?\d+$

satya - 6/1/2014 9:21:07 PM

Use the regex test method to match the exact input

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

satya - 6/1/2014 9:24:12 PM

Here is a fraction format


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

satya - 6/1/2014 9:28:20 PM

what they allow and not allow


//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

satya - 6/1/2014 9:35:51 PM

Example


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;
}

satya - 6/1/2014 9:36:43 PM

You can optimize the above code with function objects

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

satya - 6/1/2014 9:41:11 PM

You can also use groups in the regular expression

to further evaluate the portions of the number.

satya - 6/2/2014 10:00:46 AM

Here is a list of examples and standard formats

Here is a list of examples and standard formats

satya - 6/2/2014 10:05:53 AM

Another nice intro from Mozilla

Another nice intro from Mozilla

satya - 6/2/2014 10:19:42 AM

Beware of the need for double backslashes


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

//when all I needed was

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

satya - 6/2/2014 10:22:17 AM

I could have done it this way


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

//instead of 

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

satya - 6/2/2014 10:25:29 AM

So the previous testTextAnswerFormat will not work. Corrected one here


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;
}