Validation and Methods


Validation is one of the most important factors that reduce the logic flow of a software. In cases where validation is not set up correctly, it can prolong your work instead of helping you, so validation is among the most important issues. 


Validation is one of the important factors in the software process. I can even say that it is an important issue that comes first, depending on the situation. In transactions made without verifying the data, the system may run into a serious problem as well as disintegration, that is, consistency. That's why validating data is a very important issue. In addition, since the data validation process will direct the operations when the data is not correct, it does the job at the beginning without entering the business logic part.

Data validation occurs in two stages on websites, client side validation and server side validation.


Client Side Authentication

Client-side authentication, as the name suggests, covers the authentication methods on the browser. For client-side validation, your job is obviously a bit easier. For this, you can use both HTML5's own validation methods and extra js libraries.

Validation with HTML5

Actually, you don't need to do anything for this process. Of course, if you fill in the type of inputs and their values such as max, min, maxlength, minlength. In other words, if you have described an input correctly, your browser will give the rules for you. Of course, there are some extras. If the field must be full, you will need to use the required attribute. For more details, you can refer to https://www.w3schools.com/tags/tag_input.asp.

Also, sometimes classical things are not enough, such as TR Identity number. It is an 11-digit text, I draw your attention, not a number, but a text consisting of numbers. If you introduce it as a number it will probably put an indicator to the right of the input that you can increment by one, we don't want that. So what should we do? Of course we should use pattern, it is possible to give a pattern to the pattern attribute with regex. So you can also create custom rules.

Validation with JS

This is my favorite method. Because with this method, it is possible to give delicious messages. You may need to use this method, which is open to extreme customization. Sometimes it can even be like this; either enter a identity card or a passport! How can you validate with HTML5?

I actually recommend using a ready-made validation library for this process. This will save you from writing a lot of code (we prefer vanilla.js as the workcube).


Server Side Authentication

Server-side validation is used to ensure that validation works if the client's authentication mechanism is turned off (inadvertently or deliberately). It is generally possible to find libraries according to the infrastructure used. Data must be validated before entering the logic layer. If the authentication fails, either return to the user or proceed with the default values.

Verification Techniques

There are two types of verification. The first is for a data to be subject to more than one rule (classic validation), and the second is for finding the first faulty rule in a stream (fluent validation). In client-based rules, it is usually reasonable to leave the workload to the browser by performing classical validation. All errors should be displayed. However, fluent validation is a logical choice in server-side validation. Because when the server discovers the first error, it should return to the user and should not tire the central processor more.

For Example;

if (len(attributes.tckimlik) neq 11) {
arrayAppend(errors,"ID No. must be 11 Digits");
}
if (reFind("^[0-9]$", attributes.tckimlik) eq 0) {
arrayAppend(errors, "National Identity Number should only consist of numbers!");
}

We have found the errors with a classical validation above. As you can see, all errors are calculated and transferred to the error directory and the user is returned. This approach consumes more CPU. Especially if the ID is not 11 digits, it has to be queried and see if this record exists!

if (len(attributes.tckimlik) neq 11) {
errors = "ID No. must be 11 Digits";
} else if (reFind("^[0-9]+$", attributes.tckimlik) eq 0) {
errors = "National Identity Number should only consist of numbers!";
}

Here errors are not a string but a variable, so when the first error is found, else ifs will not work and the user will return the first error.

You can solve complex validation processes browser-side using the validate.js library at https://validatejs.org/. You will understand the rules better when you see the examples on the site. There is no stable validation library for Coldfusion yet, so you will have to write it by hand. However, if you write your validation codes functional, you can at least operate according to the command pattern.

if (validator.notValid(rangeLenValidator, attributes.tckimlik, 11, 11)) {
errors = "ID No. must be 11 Digits";
} else if (validator.notValid(regexValidator, attributes.tckimlik, "^[0-9]+$")) {
errors = "National Identity Number should only consist of numbers!";
}

Thus, it is possible to reuse validators according to the rules you encounter.


Feedback

Did you find this content helpful?