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

Jack SanfordJack Sanford San Antonio, TX 💎💎
edited January 12, 2018 in Questions
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");  }

Comments

  • DaveDave 💎
    edited January 12, 2018
    In case it's an option you could  use a SF validation to accomplish it


  • Jack SanfordJack Sanford San Antonio, TX 💎💎
    edited May 25, 2016
    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. 
  • edited January 12, 2018

    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"); }
  • Jack SanfordJack Sanford San Antonio, TX 💎💎
    edited May 25, 2016
    Thanks Zach!! Worked great. Just curious, what does the 'true' do on 
    var oppAccountNo = oppModel.getFieldValue(oppRow,'AccountNo__c',true);
  • Rob HatchRob Hatch 🛠️ 
    edited September 1, 2016
    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.
  • Barry SchnellBarry Schnell 💎💎
    edited February 25, 2017
    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.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!