Created
May 1, 2015 15:33
-
-
Save nulayuhz/5e379598e3a532d37e8c to your computer and use it in GitHub Desktop.
allow only number and decimal in input[type=text], comma separate on blur, convert comma separate on focus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//set number to zero if it's null or empty for number field. | |
function numberFieldChangedEvent(field, fieldName, model, invalid) { | |
field = field.replace(/[^(0-9.)]/g, ''); //allow only numbers and dots | |
model[fieldName] = field; | |
} | |
function commaSeparateNum(field, fieldName, model) { | |
// remove extra dots | |
var tempString = field.toString(); | |
if (tempString.indexOf('.') !== -1) { | |
var beforeDot = tempString.substring(0, tempString.indexOf('.')+1); | |
var afterDot = tempString.substr(tempString.indexOf('.')+1); | |
afterDot = afterDot.split('.').join(''); | |
var tempNum = parseFloat(beforeDot + afterDot); | |
model[fieldName] = $filter('number')(tempNum, 5); | |
} else { | |
model[fieldName] = $filter('number')(field, 5); | |
} | |
}; | |
function convertToNum(field, fieldName, model) { | |
model[fieldName] = parseFloat(field.replace(/,/g,'')); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment