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
June 17, 2010 at 10:25 pm |
You are the man!
June 18, 2010 at 4:41 pm
OK Larry. Do tell. What are you using this solution for?
Charlie Epes
June 18, 2010 at 12:20 am |
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?
June 18, 2010 at 9:05 am
Code updated.
Alexander
May 11, 2011 at 7:57 pm |
it does not work for me
larry or Alex can you help me?
May 15, 2011 at 8:52 pm
Hi,
Did you put the CEWP below the form?
Alexander
February 20, 2012 at 9:09 am |
Doesnt work for me and I have it in a cewp…
March 1, 2012 at 12:17 am
Have you put the CEWP below the list form?
Have your form been modified in SharePoint Designer?
Alexander
April 13, 2012 at 11:15 pm |
This is awesome! Exactly what I’m looking for to validate SIN numbers and Phone numbers! Thank you!