Saturday 2 January 2010

Validating

Validation is just a check for a certain piece of information and it usually done when focus is lost from a field. It is a critical part of any design as the cornerstone of most designs consists of information being placed into a database through these forms, and the data has to be in the correct format before it goes in. Generally there are validation checks being carried out in the Database but it can be helpful to catch any problems before this point within the application design.

For instance you may check that an email address has the '@' in it and say a certain prefix of yahoo, hotmail, google, netmail or any other web based email providers.

So for instance if you wanted to check if an email had the '@' symbol in it then in a validation event for the email field you would say:

If InStr(txtEmail.Text, "@") = 0 Then

MsgBox("The email must include the '@' symbol. " )

End if

The InStr operator checks 'In the String' for the value and the parameters you put in brackets are the (txtemail.text) field, and then what you want to find in the string i.e. the '@' symbol.

If the '@' value wasn't present then you would want to return the focus to the field until the value does include a '@' symbol. Within the code you would set the focus to the field if the check failed. This would return the user to that field until the check passed. If you did not do this then the user could move on and leave an unvalid value in the field. So the code would look like this:


If InStr(txtEmail.Text, "@") = 0 Then

MsgBox("The email must include the '@' symbol. " )
txtEmail.Focus()

End if

InStr returns a numeric value so if the value is 0 then this means the '@' symbol wasn't found.

You can do any checks at validation, this example is only one type of check.

No comments:

Post a Comment