Numbers only in single line text field

18.06.2010 – small update to set “lengthOfInputNumber” relative to the array of FieldInternalnames.

By request from Larry, here is a solution that restricts input in a single line text field to number only.

Note: It is treated as text in SharePoint and you cannot sum or average in a list view.

Add this code to a CEWP below the list form in your NewForm or EditForm:
<script type="text/javascript" src="/test/English/Javascript/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
fields = init_fields_v2();

// Allow numbers only in text fields
// Array of FieldInternalNames to lilmit to numbers only
var arrToCheckForNum = [‘Title’,’Num’];
// Array of length of input string – set to 255 if you do not want to limit input length. Corresponds to the same array index in the "arrToCheckForNum"
var lengthOfInputNumber = [9,2];

var thisVal = $(this).val();
thisVal = thisVal.substring(0,lengthOfInputNumber[idx]);
var thisVal = $(this).val();
thisVal = thisVal.substring(0,lengthOfInputNumber[idx]);

LastMod: 07.05.2010
function init_fields_v2(){
var res = {};
var myMatch = $(this).html().match(/FieldName="(.+)"s+FieldInternalName="(.+)"s+FieldType="(.+)"s+/);
// Display name
var disp = myMatch[1];
// FieldInternalName
var fin = myMatch[2];
// FieldType
var type = myMatch[3];
// Build object
res[fin] = this.parentNode;
res[fin].FieldDispName = disp;
res[fin].FieldType = type;
return res;

The parameters “arrToCheckForNum” and “lengthOfInputNumber” must be adapted to suite your needs.

The jQuery-library is found here. The pictures and the sourcecode refers to jquery-1.4.2.min. If you use another version, remember to update the script “src”.


10 thoughts on “Numbers only in single line text field”

  1. It works more than perfect.

    Just add the following code at the beginning and the end:

    // Start: Important addition 1 to make it work
    jQuery(function ($) {
    // Stop: Important addition 1 to make it work

    // Start: Important addition 2 to make it work
    // Stop: Important addition 2 to make it work

    Have a nice end of the world together. 🙂

Leave a Reply