Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have written my own Validator class a few different ways. Validation in nature doesn't necessarily require an instantiated object, so I created a class with static methods to do validation. I have used one validator method where you have to pass a type in (e.g. Email, First Name, website...), or multiple methods each for the given types. In the end, there is really only one algorithm that I needed, so I went with the one method approach. Essentially, there are class properties to hold your validation regular expressions for each type, as well as an associated error message for the given type. It all equates to a class similar to the one below:</p> <pre><code>class Validation { // define the properties for dealing with different type validations public static $firstNamePattern = '/[-a-zA-Z0-9._ ]{2,}/'; public static $lastNamePattern = '/[-a-zA-Z0-9._ ]{2,}/'; // ... more fields public static function validateText($type, $text, $fieldName) { $pattern = $type."Pattern"; if ($this-&gt;$pattern != '') { // perfom the validation // ... return true; // or false } } // other validation methods below // ... } </code></pre> <p>Then you can call that method from anywhere you need to (e.g. while validating form input).</p> <pre><code>if (Validation-&gt;validateText('firstName', $formFirstName, 'First Name')) { // validation passed } else { // validation failed } </code></pre> <p><em>I apologize the above is written in PHP and the question was about ASP, but you get my drift.</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload