Using Before Save / Field validation

Questions related to customising nuBuilder Forte with JavaScript or PHP.

Using Before Save / Field validation

Unread postby icoso » Thu Jun 17, 2021 9:44 am

I have some fields on my form that I want to make sure that they are completely filled in according to a certain format. They can't be entered partially. They must be a certain length and format BEFORE I can save them to the database.
For example a phone number.
I have a field for phone number that MUST be entered as ###-###-#### I do not want them to enter the number as ###-####.

Where in the form can I put the code to check the length and format of this field BEFORE SAVING it and if it is not entered exactly according to the format, I do not want to allow them to save the data. Should this be done in JAvascript (The forms custom code) or should this be done int he BEFORE SAVE code (which is PHP - sever side) ALL I want to happen is if the user clicks the save buttonm then the validation of the screen data occurs. if it fails, then I want to return to the edit screen with a javascript alert displayed.

I've done this many times with my own custom forms that I've built, using java script, but I don't know how to do it with the SAVE button in nuBuilder running its own function. How can I run this javascript to cancel the Save function when the user clicks it if my field validation fails?
Last edited by kev1n on Thu Jun 17, 2021 3:07 pm, edited 1 time in total.
Reason: Updated subject, removed upper case
icoso
 
Posts: 181
Joined: Mon Feb 08, 2021 7:39 am
nuBuilder Version: 4.5

Re: Using Before Save / Field validation

Unread postby kev1n » Thu Jun 17, 2021 1:11 pm

Use either the JavaScript nuBeforeSave() to do a client-side validation. Check out the Code Library for a snippet. There's event a minimal example in (Builders->) "Code Snippets"

Since a client-side validation is not secure (JavaScript can be modified or bypassed if you know how), consider a server-side validation.
Use the BS (Before Save) event and call nuDisplayError() to show an error when the validation fails.
kev1n
nuBuilder Team
 
Posts: 2059
Joined: Mon Oct 15, 2018 2:13 am
nuBuilder Version: 4.5

Re: USING BEFORE SAVE

Unread postby kev1n » Thu Jun 17, 2021 1:50 pm

kev1n wrote:Use either the JavaScript nuBeforeSave() to do a client-side validation. Check out the Code Library for a snippet. There's event a minimal example in (Builders->) "Code Snippets"

Since a client-side validation is not secure (JavaScript can be modified or bypassed if you know how), consider a server-side validation.
Use the BS (Before Save) event and call nuDisplayError() to show an error when the validation fails.


In both cases, this is going to happen:
if it fails, then I want to return to the edit screen with a javascript alert displayed.
kev1n
nuBuilder Team
 
Posts: 2059
Joined: Mon Oct 15, 2018 2:13 am
nuBuilder Version: 4.5


Return to Custom Code

Who is online

Users browsing this forum: No registered users and 3 guests