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

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 &amp;&amp; boolean === false) { $("#sk-KX6Zo-617").html(""); } //else if (result.fail){ // $("#sk-KX6Zo-617").html("Please enter numbers only"); //} else //if (oppString.length !== 10 &amp;&amp; oppString.matches("[a-zA-Z]+") === true) { $("#sk-KX6Zo-617").html("Account numbers should be 10 digits long and contain only numbers"); }<br>

In case it’s an option you could  use a SF validation to accomplish it


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. 

<br />
var oppModel = skuid&#46;$M('Opportunity'); var oppRow = oppModel&#46;getFirstRow(); var oppAccountNo = oppModel&#46;getFieldValue(oppRow,'AccountNo__c',true); if (!/^d{10}$/&#46;test(oppAccountNo)) { skuid&#46;$("#sk-KX6Zo-617")&#46;html("Account numbers should be 10 digits long and contain only numbers"); }

Thanks Zach!! Worked great. Just curious, what does the ‘true’ do on 

var oppAccountNo = oppModel.getFieldValue(oppRow,'AccountNo__c',<b>true</b>);

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.

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.