Important Update: Community URLs redirect issues are partially resolved. Learn More. .

cancel
Showing results for 
Search instead for 
Did you mean: 

Javascript Validator Triggered by Change in Text Field

JeffLamb1
Contributor III

I'm trying to create a custom object that will trigger on a change to the contents of a text (not text area) field. The goal is to display a warning when the contents of the text field do not match a specific format. I can get the function to fire based on other field types (like a date/time field or a dropdown values list field) but can't get it to trigger based on a change to a text field.

The code below shows what I'm trying to do. The validation function is just a simplified example. The first event handler is triggered by a change in a date/time field and is just there to verify a successful call to the validator. It works but is not what we are looking for. The second handler is an attempt to be a trigger of a change in a text field. That's what we really want, but it is not working.

Any help is appreciated.

<script type="text/javascript">
const TestDateTimeFldId = 11111; //Test Date Time
const TestTextFldId = 22222; //Test Text
 
 
function validateTextLength(fldid) {
const FieldValue = $CM.getFieldValue(fldid);
if (FieldValue.length == 19) {
return true;
} else {
return false;
}
};
 
Sys.Application.add_load(function() {
 
// Event handler for change to clock field
$('div[id*="f' + TestDateTimeFldId + 'cdp"]').change(function() { // this works
  const FieldValue = $CM.getFieldValue(TestTextFldId);
if (validateTextLength(TestTextFldId)) {
WarningAlert(FieldValue,'Valid Text - triggered by clock widget');
} else {
WarningAlert(FieldValue,'Invalid Text - triggered by clock widget');
}
});
 
 
// Event handler to change to text field. NOT WORKING YET
$('div[id*="f' + TestTextFldId + 'c"]').change(function() { // this doesn't work
const FieldValue = $CM.getFieldValue(TestTextFldId);
if (validateTextLength(TestTextFldId)) {
WarningAlert(FieldValue,'Valid Text - triggered by text');
} else {
WarningAlert(FieldValue,'Invalid Text - triggered by text');
}
});
 
});
</script>

 

 

13 REPLIES 13

DavidPetty
Archer Employee
Archer Employee

@JeffLamb1 try, just update the field id (23319).

<script>
     Sys.Application.add_load(function() {
          $('#master_DefaultContent_rts_s8352_f23319c').blur(function() {
               //do something
          });
     });
</script>

 

 Advisory Consultant

I will try that. One question first; what does the s8352 represent? When I inspect my field, I see a different number after s.

Anonymous
Not applicable

@JeffLamb1 sorry, meant that needs updated as well.  

Or go wit this approach, The Horror's of Moving Custom Objects from One Environment to Another - Archer Community - 569029 (archerirm.community)

 Advisory Consultant

That event handler works, so thank you!

One additional minor question. If my validator returns false, is there an easy way to make the focus return to the text field after the user clicks OK on the warning message?

: D

You could use (jQuery) .focus() on that element.

 Advisory Consultant

Sorry, would the syntax on that be this?

$('#master_DefaultContent_rts_s8352_f23319c').focus();

That should do the trick ; )

 Advisory Consultant

JeffLamb1
Contributor III

When I do that, it whites-out the entire record page. Just keeps the Warning dialog box displayed over a white page. And won't let me close the dialog box either.

$('#master_DefaultContent_rts_s8352_f23319c').blur(function() {
if isInvalid(23319)) {
WarningAlert(WarningMsg,'Invalid');
$('#master_DefaultContent_rts_s8352_f23319c').focus();
}
});