Snippet to check length needs to also make sure there are no letters only

  • 1
  • Question
  • Updated 3 years ago
  • Answered
I've got a snippet that is run every time a field is updated to check whether that field has 10 digits or not. I'd like to to make sure that there are no letters in there as well. Right now, it's only counting numbers so if I xyz1234567890xyz it's a pass, but x123456789 is a fail.

Is there a way I could have xyz1234567890 fail as well, and maybe give a different error?

I've tried so many options from searching javascript forums, like .matches, .find, .test, and I keep getting "is not a function" errors in the console. I did not get error using .test, but it also isn't able to detect when there's a letter in the field.

here's the code I've got now, that functions to check for number of numbers, but doesn't count letters in .length . I've commented out a lot of other attempts. 

var params = arguments[0],
	$ = skuid.$;

var oppModel = skuid.model.getModel('Opportunity');
var oppRow = oppModel.getFirstRow();
var oppAccountNo = oppModel.getFieldValue(oppRow,'AccountNo__c');
var oppString = oppAccountNo.toString();
var boolean = /[a-z]/i.test(oppString);
//var accountNo = Number("oppAccountNo");
//var nolength = oppAccountNo.length;
//var Integer = Integer.new;
//var result = parseInt(oppAccountNo);

if (oppString.length === 10 && boolean === false) {
     $("#sk-KX6Zo-617").html("");
} 
//else if (result.fail){
//    $("#sk-KX6Zo-617").html("Please enter numbers only");
    
//}
else 
//if (oppString.length !== 10 && oppString.matches("[a-zA-Z]+") === true)
{
    $("#sk-KX6Zo-617").html("Account numbers should be 10 digits long and contain only numbers");
}
Photo of Jack Sanford

Jack Sanford, Champion

  • 8,270 Points 5k badge 2x thumb

Posted 3 years ago

  • 1
Photo of Dave

Dave

  • 5,538 Points 5k badge 2x thumb
In case it's an option you could  use a SF validation to accomplish it
Photo of Jack Sanford

Jack Sanford, Champion

  • 8,270 Points 5k badge 2x thumb
It's not unfortunately, this AccountNo__c field is used by several different clients in several different ways, and some of them have letters in their account numbers. 
Photo of Zach McElrath

Zach McElrath, Employee

  • 48,984 Points 20k badge 2x thumb

var oppModel = skuid.$M('Opportunity');
var oppRow = oppModel.getFirstRow();
var oppAccountNo = oppModel.getFieldValue(oppRow,'AccountNo__c',true);
if (!/^\d{10}$/.test(oppAccountNo)) {
skuid.$("#sk-KX6Zo-617").html("Account numbers should be 10 digits long and contain only numbers");
}
(Edited)
Photo of Jack Sanford

Jack Sanford, Champion

  • 8,270 Points 5k badge 2x thumb
Thanks Zach!! Worked great. Just curious, what does the 'true' do on 
var oppAccountNo = oppModel.getFieldValue(oppRow,'AccountNo__c',true);
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
From the doc at:  http://help.skuidify.com/m/11720/l/205447-skuid-model-model

The last parameter for getFieldValue: 
  • noEscape (boolean): Optional. If true, Skuid will not HTML-escape the field value.
Photo of Barry Schnell

Barry Schnell, Champion

  • 18,076 Points 10k badge 2x thumb
Hi Jack -

In addition to the approach Zach outlines, you can take a proactive approach that would prohibit any non-digit from being input in to the text field.  There are a number of plug-ins available that would accomplish this.  The one I'm using is AlphaNumericPlus.  In a custom renderer, you can attach the plugin setting its options as you need and it will protect the field from a user inputting invalid characters.  Check it out at  https://www.nuget.org/packages/AlphaNumericPlus/.

Combining the reactive vs. proactive approaches would give you foolproof protection.