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

$.each(arrToCheckForNum,function(idx,item){
$(fields[item]).find('input').css({'width':'75px'});
	$(fields[item]).find('input').keyup(function(e){
		var thisVal = $(this).val();
		thisVal = thisVal.substring(0,lengthOfInputNumber[idx]);
		$(this).val(thisVal.replace(/[^0-9]/g,''));
	}).blur(function(){
		var thisVal = $(this).val();
		thisVal = thisVal.substring(0,lengthOfInputNumber[idx]);
		$(this).val(thisVal.replace(/[^0-9]/g,''));
	});
});

/*
  LastMod: 07.05.2010
*/
function init_fields_v2(){
	var res = {};
	$("td.ms-formbody").each(function(){
	var myMatch = $(this).html().match(/FieldName="(.+)"\s+FieldInternalName="(.+)"\s+FieldType="(.+)"\s+/);	
		if(myMatch!=null){
			// Display name
			var disp = myMatch[1];
			// FieldInternalName
			var fin = myMatch[2];
			// FieldType
			var type = myMatch[3];
			if(type=='SPFieldNote'){
				if($(this).find('script').length>0){
					type=type+"_HTML";
				}
			}
			if(type=='SPFieldLookup'){
				if($(this).find('input').length>0){
					type=type+"_Input";
				}
			}
			// Build object
			res[fin] = this.parentNode;
			res[fin].FieldDispName = disp;
			res[fin].FieldType = type;
		}		
	});
	return res;
}
</script>

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”.

Alexander

9 Responses to “Numbers only in single line text field”

  1. larry Says:

    You are the man!

  2. larry Says:

    the only question I have about this. Does the lengthOfInputNumber apply to fields, or can it be setup like an array to match field array?

  3. SPKID Says:

    it does not work for me

    larry or Alex can you help me?

  4. Tony Says:

    Doesnt work for me and I have it in a cewp…

    • Alexander Bautz Says:

      Have you put the CEWP below the list form?
      Have your form been modified in SharePoint Designer?

      Alexander

  5. Allen L. Says:

    This is awesome! Exactly what I’m looking for to validate SIN numbers and Phone numbers! Thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 435 other followers