I put together a generic Javascript that facilitates the automated focus of an input field on a form.
It checks for a field without a value and sets focus on that field, add add a class of "error" and returns. However, that is valid only if the field is required.
Addtionally, I did not want to touch an engine file, I created a short PHP function (also on the view file), that checks for an error on the field and sets the error message as the label, while altering the message to remove verbosity for each field by calling the function.
Since we set the rules in a function within a PHP class, the fact that the field is required is not apparent in the HTML. And that makes it difficult to know if the field is required from within a Javascript on the view file. Any ideas?
Together this is what that looks like.
Javascript in a view file, need advice...
1 months ago
1 months ago
#1
1 months ago
#2
Well, I stared at it for another 15 minutes, and it occurred to me that there is no need to determine if the input field is empty, or if it was required. If both of those things are true, there will be an error passed back from the validation module. BAZINGA!!
The fix is to remove the first "if" entirely from the script.
Abda Abda Abda That's all folks!!
The fix is to remove the first "if" entirely from the script.
Abda Abda Abda That's all folks!!
1 months ago
#3
You can simplify this further by letting the browser handle required validation and using CSS/JS only to react to it. If you add the `required` attribute to fields, the browser automatically tracks invalid inputs.
Then your script only needs to focus on the first invalid field.
Example HTML:
Advantages:
• No need to detect empty values manually
• No looping through labels
• Works with built-in browser validation
• Server validation remains the authority
The browser already knows which fields are invalid, so the JavaScript only reacts to that state.
Then your script only needs to focus on the first invalid field.
Example HTML:
Advantages:
• No need to detect empty values manually
• No looping through labels
• Works with built-in browser validation
• Server validation remains the authority
The browser already knows which fields are invalid, so the JavaScript only reacts to that state.
1 months ago
#4
★ Winning Post! ★
The thread creator has indicated that this post solved the problem or provided the best insight.
Members who post winning responses earn points for our leaderboard.
Points mean prizes!
1 months ago
#5
Hail DaFa,
Thank you, sir.
The thought did cross my mind, to flag the field as required in the HTML, and allow the browser to handle the validation, along with minlength and maxlength, min and max values, and a type clause; and do away with the Trongate validation run. So, why are we using the Trongate validation function?
Also, the default browser validation tooltips are really fugly! Is there any way to alter the rendering?
Well, I'll be a monkey's uncle... I found a short tutorial on this very subject.
https://www.tutorialpedia.org/blog/how-to-set-custom-validation-messages-for-html-forms/
And it looks like a major faff. But I have nothing but times on my hands. I haven't made nickel from coding in the last ten years. Again, thank you. It's nice to have good people that I can bounce things off of.
BTW, how do you get the button to close the thread?
Thank you, sir.
The thought did cross my mind, to flag the field as required in the HTML, and allow the browser to handle the validation, along with minlength and maxlength, min and max values, and a type clause; and do away with the Trongate validation run. So, why are we using the Trongate validation function?
Also, the default browser validation tooltips are really fugly! Is there any way to alter the rendering?
Well, I'll be a monkey's uncle... I found a short tutorial on this very subject.
https://www.tutorialpedia.org/blog/how-to-set-custom-validation-messages-for-html-forms/
And it looks like a major faff. But I have nothing but times on my hands. I haven't made nickel from coding in the last ten years. Again, thank you. It's nice to have good people that I can bounce things off of.
BTW, how do you get the button to close the thread?
1 months ago
#6
@Charles Luck; I'm afraid you accidentally chose the wrong post (#4) as the winning post, where - obviously - you might have meant to choose #3, as it was the most helpful in finding your answer. (no offence!)