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 =;  //var result = parseInt(oppAccountNo);    if (oppString.length === 10 && boolean === false) {       $("#sk-KX6Zo-617").html("");  }   //else if ({  //    $("#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");  }


  • 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.ย 
  • Zach McElrathZach McElrath Principal Software Engineer Chattanooga, TN ๐Ÿ’Ž๐Ÿ’Ž๐Ÿ’Ž
    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: ย

    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ย ย

    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!