Length Validation

I am creating an app for use in Australia, over here the mobile number is 10 digits long. When creating a User I want to ensure that the mobile number length is exactly 10 digits, no more no less. I can limit the maximum length of the field by entering 10 in the Field length from the doctype settings. To validate the minimum length I wrote the following script but it does not seem to work. Any help is appreciated

Script:

frappe.ui.form.on(“User”, “validate”, function(frm) {
if (frm.doc.mobile_no.length < ‘10’){
frappe.msgprint(__(“Enter a valid Mobile Number”));
frappe.validated = false;
}
});

1 Like

Nevermind, got it to work by removing the comas from 10. However another issue has risen now, Alongside Mobile Number I wish to pass the same validation for Phone Number. If i write another If condition in this script, it doesn’t work. Any ideas as to how to pass two validate functions in one script?

It should work with 2 if statements. Can you provide your code?
And checking for
<>10
instead of
<10
may be more appropriate along with the numeric char control.

Code:
frappe.ui.form.on(“User”, “validate”,function(frm) {
if (frm.doc.phone_number.length != 10){
frappe.msgprint((“Enter a valid phone Number”));
frappe.validated = false;
}
if (frm.doc.mobile_number.length != 10){
frappe.msgprint(
(“Enter a valid Mobile Number”));
frappe.validated = false;
}
});

I also tried :
frappe.ui.form.on(“User”, “validate”,function(frm) {
if (frm.doc.phone_number.length != 10 && frm.doc.phone_number.length != 10){
frappe.msgprint(__(“Enter a valid phone/mobile Number”));
frappe.validated = false;
}
});

That didnt work either. However I would prefer if i could pass multiple If conditions as there are other validation checks I need to pass too.

OR (||) instead of AND (&&) required for this example.

If you check for mobile_number only, does it work?

Yeah it works for a single condition

Ill try using OR and AND instead of the characters, however There is another field I wish to validate. Is there not any way I could pass Multiple IF conditions rather than multiple conditions in one IF condition? Like the code I wrote with two IF’s?

Nope Doesn’t work. This is the error log:

Error in Custom Script

SyntaxError: Unexpected identifier
    at init.setup (http://vertex360.com.au/assets/js/form.min.js?ver=1612317133.0:1:1128580)
    at frappe.ui.form.Form.setup (http://vertex360.com.au/assets/js/form.min.js?ver=1612317133.0:1:1178920)
    at frappe.ui.form.Form.refresh (http://vertex360.com.au/assets/js/form.min.js?ver=1612317133.0:1:1182070)
    at frappe.views.FormFactory.render (http://vertex360.com.au/assets/js/form.min.js?ver=1612317133.0:1:1086895)
    at http://vertex360.com.au/assets/js/form.min.js?ver=1612317133.0:1:1086606
    at Object.callback (http://vertex360.com.au/assets/js/desk.min.js?ver=1612317133.0:1:160924)
    at Object.success [as success_callback] (http://vertex360.com.au/assets/js/desk.min.js?ver=1612317133.0:1:61576)
    at 200 (http://vertex360.com.au/assets/js/desk.min.js?ver=1612317133.0:1:61881)
    at Object. (http://vertex360.com.au/assets/js/desk.min.js?ver=1612317133.0:1:64479)
    at i (http://vertex360.com.au/assets/frappe/js/lib/jquery/jquery.min.js:2:27151)

This is my code:

frappe.ui.form.on(“User”, “validate”,function(frm) {
if (frm.doc.phone_number.length != 10 AND frm.doc.mobile_number.length != 10){
frappe.msgprint(__(“Enter a valid phone Number”));
frappe.validated = false;
}
});

Did you try multiple if statement with 10 char phone number and 11 char mobile number? It is plain JS, it should work.
You may also try something like

frappe.ui.form.on(“User”, “validate”,function(frm) {
let strDesc = “”;
if (frm.doc.phone_number.length != 10){
strDesc = "Enter a valid phone Number.”;
frappe.validated = false;
}
if (frm.doc.mobile_number.length != 10){
strDesc += "Enter a valid Mobile Number.”;
frappe.validated = false;
}

if (frappe.validated == false)
frappe,msgprint(strDesc);
});

You should use || notation.

error log for this:

Error in Custom Script

SyntaxError: Invalid or unexpected token
    at init.setup (http://vertex360.com.au/assets/js/form.min.js?ver=1612317133.0:1:1128580)
    at frappe.ui.form.Form.setup (http://vertex360.com.au/assets/js/form.min.js?ver=1612317133.0:1:1178920)
    at frappe.ui.form.Form.refresh (http://vertex360.com.au/assets/js/form.min.js?ver=1612317133.0:1:1182070)
    at frappe.views.FormFactory.render (http://vertex360.com.au/assets/js/form.min.js?ver=1612317133.0:1:1086895)
    at http://vertex360.com.au/assets/js/form.min.js?ver=1612317133.0:1:1086606
    at Object.callback (http://vertex360.com.au/assets/js/desk.min.js?ver=1612317133.0:1:160924)
    at Object.success [as success_callback] (http://vertex360.com.au/assets/js/desk.min.js?ver=1612317133.0:1:61576)
    at 200 (http://vertex360.com.au/assets/js/desk.min.js?ver=1612317133.0:1:61881)
    at Object. (http://vertex360.com.au/assets/js/desk.min.js?ver=1612317133.0:1:64479)
    at i (http://vertex360.com.au/assets/frappe/js/lib/jquery/jquery.min.js:2:27151)

Doesnt work. It doesnt even let me save the page (nothing happens when i click on save, no save, no error message). Here’s the code:

frappe.ui.form.on(“User”, “validate”,function(frm) {
if (frm.doc.phone_number.length != 10 || frm.doc.mobile_number.length != 10){
frappe.msgprint(__(“Enter a valid phone Number”));
frappe.validated = false;
}
});

else
frappe.validated = true;

Still doesn’t let me save. No message, no error, nothing

Maybe it is the " that is got smart-converted to prettified?
Can you repost the code and put it inside ``` so easier to see?
I copy and paste your above code here:

frappe.ui.form.on(“User”, “validate”,function(frm) {
if (frm.doc.phone_number.length != 10 || frm.doc.mobile_number.length != 10){
frappe.msgprint(__(“Enter a valid phone Number”));
frappe.validated = false;
}
});
       if (frm.doc.phone_number.length != 10 || frm.doc.mobile_number.length != 10){
           frappe.msgprint(__("Enter a valid phone Number"));
           frappe.validated = false;
       }
       else
       frappe.validated = true;
});

Also tried this code:

        if (frm.doc.phone_number.length != 10 || frm.doc.mobile_number.length != 10){
            frappe.msgprint(__("Enter a valid phone Number"));
            frappe.validated = false;
        }
});

Reread your code and question.
What do you try to achieve in validating the digit:

  • mobile number, or
  • phone number ?

Are they in the same or different form fields?

If it works for single condition, why not doing it separately?
Or nested (if they are related).

Also try using the (each) fieldname instead of validate for trigger.

1 Like

Try this code.
frappe.ui.form.on (“Usuario”, “validar”, función (frm) {
if (frm.doc.phone_number.length<10) {
msgprint=("Enter a valid phone Number ”);
validate= false;
}
if (frm.doc.mobile_number.length<10) {
msgprint=("Enter a valid mobile Number ”);
validate= false;
}
)};