ERPNext.com Frappe Cloud Support Partners Foundation Frappe School

How to call a common Js Validation file

I have a custom validation file like as custome_validation.js. This file contains mail validation and phone number validation.
I include that file in Hooks like (app_include_js=“assets/js/Custome_validation.js”). I have list of functions like

function email_validation(fieldname)
{statements
}

  1. How to call this function where ever in need.
  2. How to focus this field.
  3. How to set onkey press event for particular doc name.

You should do these changes at the framework level, or you can monkey-patch by listening on the form_refresh event

What doesnt work

I was adding jQuery to a web page. Adding the HTML script tag to the header field doesn’t work within the Website Settings and Web Page (worse: it converts the HTML to text) doctype.


Website settings (doesn’t work)


Web Page (doesn’t work)

Solution

So a workaround was to add it within the Web Page JavaScript field using jQuery.getScript(). I activated caching too in my solution.

$(document).ready(function() {

jQuery.cachedScript = function( url, options ) {
  // Allow user to set any option except for dataType, cache, and url
  options = $.extend( options || {}, {
    dataType: "script",
    cache: true,
    url: url
  });

  return jQuery.ajax( options );
};

$.cachedScript( "https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.min.js" )
  .done(function( script, textStatus ) {
    console.log( textStatus );

    // my code goes here

});
});

Credit to @snv

1 Like