Documente Academic
Documente Profesional
Documente Cultură
Validation Rules
Validation rules prevent bad data being saved in your table. Basically, they look like
criteria in a query.
You can create a rule for a field (lower pane of table design), or for the table (in the
Properties box in table design.) Use the table's rule to compare fields.
There is one trap to avoid. In some versions of Access, you will not be able to leave
the field blank once you add the validation rule, i.e. you must enter something that
satisfies the rule. If you need to be able to leave the field blank, add OR Is Null to
your rule. (Some versions accept Nulls anyway, but we recommend you make it
explicit for clarity and consistency.)
This article explains how to use validation rules, and concludes with some thought
provoking on when to use them.
When you select a field in table design, you see its Validation Rule property in the
lower pane.
This rule is applied when you enter data into the field. You cannot tab to the next
field until you enter something that satisfies the rule, or undo your entry.
Examples:
To do this ... Validation Rule for Fields Explanation
Accept letters (a - z) only Is Null OR Not Like "*[!a-z]*" Any character outside the
range A to Z is rejected. (Case insensitive.)
Accept digits (0 - 9) only Is Null OR Not Like "*[!0-9]*" Any character outside the
range 0 to 9 is rejected. (Decimal point and negative sign rejected.)
Letters and spaces only Is Null Or Not Like "*[!a-z OR "" ""]*" Punctuation and
digits rejected.
Digits and letters only Is Null OR Not Like "*[!((a-z) or (0-9))]*" Accepts A to
Z and 0 to 9, but no punctuation or other characters.
Exactly 8 characters Is Null OR Like "????????" The question mark stands for
one character.
Exactly 4 digits Is Null OR Between 1000 And 9999 For Number fields.
Positive numbers only Is Null OR >= 0 Remove the "=" if zero is not allowed
either.
(Not Like "*[ ,;]*")) Requires at least one character, @, at least one character,
dot, at least one character. Space, comma, and semicolon are not permitted.
You must fill in Field1 Not Null Same as setting the field's Required
property, but lets you create a custom message (in the Validation Text property.)
Limit to specific choices Is Null OR "M" Or "F" It is better to use a lookup table
for the list, but this may be useful for simple choices such as Male/Female.
Yes/No/Null field Is Null OR 0 or -1 The Yes/No field in Access does not support
Null as other databases do. To simulate a real Yes/No/Null data type, use a Number
field (size Integer) with this rule. (Access uses 0 for False, and -1 for True.)
Validation Rules for tables
In table design, open the Properties box and you see another Validation Rule. This is
the rule for the table.
The rule is applied after all fields have been entered, just before the record is saved.
Use this rule to compare values across different fields, or to delay validation until
the last moment before the record is saved.
Examples:
A booking cannot
([EndDate] Is Null) OR
([StartDate] <= [EndDate]) The rule is satisfied if either field is left blank;
otherwise StartDate must be before (or the same as) EndDate.
Field2 is required also ([Field1] Is Null) OR ([Field2] Is Not Null) The rule is
satisfied if Field1 is blank; otherwise it is satisfied only if Field2 is filled in.
or Field2, but not both ([Field1] Is Null) XOR ([Field2] Is Null) XOR is the
exclusive OR.
Take a BirthDate field, for example. Should you create a rule to ensure the user
doesn't enter a future date? We would need some radically different physics to ever
be entering people who are not yet born, so it sounds like a safe enough rule? But
did you consider that the computer's date might be wrong? Would it be better to
give a warning rather than block the entry?
The answer to that question is subjective. The question merely illustrates the need
to think outside the box whenever you will block data, not merely to block things
just because you cannot imagine a valid scenario for that data.
Validation Rules are absolute. You cannot bypass them, so you cannot use them for
warnings. To give a warning instead, use an event of your form, such as
Form_BeforeUpdate.
We suggested using this rule for comparing fields. In the ideal database design, the
fields are not dependent on each other, so if you are comparing fields, you might
consider whether there is another way to design the table.
Our first example above ensures that a booking does not end before it starts. There
is therefore a dependency between these two fields. Could we redesign the table
without that dependency? How about replacing EndDate with a Duration field?
Duration would be a number in an applicable unit (e.g. days for hotel bookings,
periods for school classrooms, or minutes for doctors appointments.) We use a
calculated field in a query to get the EndDate. This may not be the best design for
every case, but it is worth considering when you go to use the record-level
validation rule.
Limitations
You want to call user-defined functions, or VBA functions beyond the ones in JET
such as IIf() and Date().
The expression involves data in other records or other tables. (Well, not easily,
anyway.)
Alternatives
Required: Setting a field's Required property to Yes forces the user to enter
something. (In addition to the obvious cases, always consider setting this to Yes for
your foreign key fields. See #3 in this article for details.)
Allow Zero Length: Setting this property to No for text, memo, and hyperlink fields
prevents a zero-length string being entered. A ZLS is not the same as a Null, so if
you permit this you have confusing data for the user, more work checking for both
as a developer, more chance of a mistake, and slower executing queries. More
information in Problem Properties.
Indexed: To prevent duplicates in a field, set this property to Yes (No Duplicates).
Using the Indexes box in table design, you can create a multi-field unique index to
the values are unique across a combination of fields.
Lookups: Rather than creating a validation rule consisting of a list of valid values,
consider creating a related table. This is much more flexible and easier to maintain.
Input Mask: Of limited use. Users must enter the entire pattern (without them you
can enter some dates with just 3 keystrokes, e.g. 2/5), and they cannot easily insert
a character if they missed one.
Conclusion
Validation rules are very useful for keeping bad data out of your tables, but be
careful not to overdo them. You don't want to block things that might be valid,
though unexpected.
Validation Rules
Validation rules prevent bad data being saved in your table. Basically, they look like criteria in a query.
You can create a rule for a field (lower pane of table design), or for the table (in the Properties box in
table design.) Use the table's rule to compare fields.
There is one trap to avoid. In some versions of Access, you will not be able to leave the field blank
once you add the validation rule, i.e. you must enter something that satisfies the rule. If you need to
be able to leave the field blank, add OR Is Null to your rule. (Some versions accept Nulls anyway, but we recommend
you make it explicit for clarity and consistency.)
This article explains how to use validation rules, and concludes with some thought provoking
on when to use them.
Validation Rule
To do this ... Explanation
for Fields
Accept letters Is Null OR Not Any character outside the range A to Z is rejected. (Case
(a - z) only Like "*[!a-z]*" insensitive.)
Accept digits Is Null OR Not Any character outside the range 0 to 9 is rejected. (Decimal point
(0 - 9) only Like "*[!0-9]*" and negative sign rejected.)
Is Null Or Not
Letters and
Like "*[!a-z OR "" Punctuation and digits rejected.
spaces only
""]*"
Is Null OR Not
Digits and
Like "*[!((a-z) or Accepts A to Z and 0 to 9, but no punctuation or other characters.
letters only
(0-9))]*"
Exactly 8 Is Null OR Like
The question mark stands for one character.
characters "????????"
Is Null OR
Between 1000 For Number fields.
Exactly 4 digits And 9999
Is Null OR Like
For Text fields.
"####"
Positive
Is Null OR >= 0 Remove the "=" if zero is not allowed either.
numbers only
Is Null OR
No more than 100% is 1. Use 0 instead of -1 if negative percentages are not
Between -1 And
100% allowed.
1
Not a future Is Null OR <=
date Date()
Is Null OR ((Like
Requires at least one character, @, at least one character, dot, at
"*?@?*.?*") AND
Email address least one character. Space, comma, and semicolon are not
(Not Like
permitted.
"*[ ,;]*"))
You must fill Same as setting the field's Requiredproperty, but lets you create a
Not Null
in Field1 custom message (in the Validation Text property.)
Is Null OR "M" Or It is better to use a lookup table for the list, but this may be useful
Limit to "F" for simple choices such as Male/Female.
specific
choices Is Null OR IN (1,
The IN operator may be simpler than several ORs.
2, 4, 8)
The Yes/No field in Access does not support Null as other
Yes/No/Null databases do. To simulate a real Yes/No/Null data type, use a
Is Null OR 0 or -1
field Number field (size Integer) with this rule. (Access uses 0 for False,
and -1 for True.)
Conclusion
Validation rules are very useful for keeping bad data out of your tables, but be careful not to overdo
them. You don't want to block things that might be valid, though unexpected.
•
•
•
•
Posted August 5th, 2010 in Web Design and tagged form validation, javascript by Mike Wagan
If you need a quick javascript form validation script, you can use this one
from Javascript-coder.com. Its pretty easy to implement and I’ve used it in some of my
projects already. Itsgonna save you some time coding from scratch!
You can download the JavaScript form validation script here.
The zip file contains the java script file, documentation and examples.
1.Include gen_validatorv31.js in your html file just before closing the HEAD tag
2. Just after defining your form, Create a form validator object passing the name of the
form
<FORM name='myform' action="">
<!----Your input fields go here -->
</FORM>
<SCRIPT language="JavaScript">
varfrmvalidator = new Validator("myform");
....
3. Now add the validations required
frmvalidator.addValidation("FirstName","alpha");
the first argument is the name of the field and the second argument is the validation
descriptor, which specifies the type of validation to be performed.
You can add any number of validations. The list of validation descriptors are provided at
the end of the documentation.
The optional third argument is the error string to be displayed if the validation fails.
frmvalidator.addValidation("FirstName","alpha");
frmvalidator.addValidation("FirstName","req","Please enter your First Name");
frmvalidator.addValidation("FirstName","maxlen=20",
"Max length for FirstName is 20");
4. Similarly, add validations for the fields where validation is required.
That’s it! You are ready to go.
The example below will make the idea clearer
<form action="" name="myform" >
<table cellspacing="2" cellpadding="2" border="0">
<tr>
<td align="right">First Name</td>
<td><input type="text" name="FirstName"></td>
</tr>
<tr>
<td align="right">Last Name</td>
<td><input type="text" name="LastName"></td>
</tr>
<tr>
<td align="right">EMail</td>
<td><input type="text" name="Email"></td>
</tr>
<tr>
<td align="right">Phone</td>
<td><input type="text" name="Phone"></td>
</tr>
<tr>
<td align="right">Address</td>
<td><textarea cols="20" rows="5" name="Address"></textarea></td>
</tr>
<tr>
<td align="right">Country</td>
<td>
<SELECT name="Country">
<option value="" selected>[choose yours]
<option value="008">Albania
<option value="012">Algeria
<option value="016">American Samoa
<option value="020">Andorra
<option value="024">Angola
<option value="660">Anguilla
<option value="010">Antarctica
<option value="028">Antigua And Barbuda
<option value="032">Argentina
<option value="051">Armenia
<option value="533">Aruba
</SELECT>
</td>
</tr>
<tr>
<td align="right"></td>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
<script language="JavaScript" type="text/javascript">
varfrmvalidator = new Validator("myform");
frmvalidator.addValidation("FirstName","req","Please enter your First Name");
frmvalidator.addValidation("FirstName","maxlen=20",
"Max length for FirstName is 20");
frmvalidator.addValidation("FirstName","alpha");
frmvalidator.addValidation("LastName","req");
frmvalidator.addValidation("LastName","maxlen=20");
frmvalidator.addValidation("Email","maxlen=50");
frmvalidator.addValidation("Email","req");
frmvalidator.addValidation("Email","email");
frmvalidator.addValidation("Phone","maxlen=50");
frmvalidator.addValidation("Phone","numeric");
frmvalidator.addValidation("Address","maxlen=50");
frmvalidator.addValidation("Country","dontselect=0");
</script>
The form validators should be created only after defining the HTML form (only after
the </form> tag. )
Your form should have a distinguished name. If there are more than one form in the
same page, you can add validators for each of them. The names of the forms and the
validators should not clash.
You can’t use the javascriptonsubmit event of the form if it you are using this
validator script. It is because the validator script automatically overrides the onsubmit
event. If you want to add a custom validation, see the section below
If you want to add a custom validation, which is not provided by the validation
descriptors, you can do so. Here are the steps:
Create a javascript function which returns true or false depending on the validation.
function DoCustomValidation()
{
varfrm = document.forms["myform"];
if(frm.pwd1.value != frm.pwd2.value)
{
sfm_show_error_msg('The Password and verified password don not match!',frm.pwd1);
return false;
}
else
{
return true;
}
}
sfm_show_error_msg() displays the error message in your chosen style. The first
parameter is the error message and the second parameter is the input object.
Associate the validation function with the validator object.
frmvalidator.setAddnlValidationFunction("DoCustomValidation");
The custom validation function will be called automatically after other validations.
If you want to do more than one custom validations, you can do all those validations in
the same function.
function DoCustomValidation()
{
varfrm = document.forms["myform"];
if(false == DoMyValidationOne())
{
sfm_show_error_msg('Validation One Failed!');
return false;
}
else
if(false == DoMyValidationTwo())
{
sfm_show_error_msg('Validation Two Failed!');
return false;
}
else
{
return true;
}
}
frmvalidator.clearAllValidations();
this function call clears all validations you set.
You will not need this method in most cases.
By default, if there is a validation error, the focus is set on the input element having the
error. You can disable this behavior by calling:
frmvalidator.EnableFocusOnError(false);
required
req The field should not be empty
maxlen=??? checks the length entered data to the maximum. For example, if the maximum size
maxlength=??? permitted is 25, give the validation descriptor as “maxlen=25″
minlen=???
minlength=??? checks the length of the entered string to the required minimum. example “minlen=5″
alphanumeric / Check the data if it contains any other characters other than alphabetic or numeric
alnum characters
alphanumeric_space
/
alnum_s Allows only alphabetic, numeric and space characters
num
numeric Check numeric data (allow only digits)
dec
decimal Allow numbers (with decimal point)
alpha
alphabetic Check alphabetic data.
alpha_s
alphabetic_space Check alphabetic data and allow spaces.
email The field is an email field and verify the validity of the data.
Verify the data to be less than the value passed. Valid only for numeric fields.
lt=??? example: if the value should be less than 1000 give validation description as
lessthan=??? “lt=1000″
gt=??? Verify the data to be greater than the value passed. Valid only for numeric fields.
greaterthan=??? example: if the value should be greater than 10 give validation description as “gt=10″
Check with a regular expression the value should match the regular expression.
regexp=??? example: “regexp=^[A-Za-z]{1,20}$” allow up to 20 alphabetic characters.
This validation descriptor is valid only for select input items (lists) Normally, the
select list boxes will have one item saying ‘Select One’ or some thing like that. The
user should select an option other than this option. If the index of this option is 0, the
dontselect=?? validation description should be “dontselect=0″
This validation descriptor is valid only for check boxes. The user should not select th
given check box. Provide the value of the check box instead of ?? For example,
dontselectchk dontselectchk=on
This validation descriptor is valid only for check boxes. The user should select th
given check box. Provide the value of the check box instead of ?? For example,
shouldselchk shouldselchk=on
selone_radio Checks whether at least one radio button is selected.
Example Page
See the JavaScript form validation example here
The JavaScript form validation script has still more advanced features. To know about
how to display the form validation error messages together, or about displaying the
form on the page itself, see: Form Validation : more features
Source: http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
Share and Enjoy:
•
•
X
Welcome Googler! If you find this page useful, you might want to subscribe
to the RSS feed for updates on this topic.
You were searching for "validation code for table". See posts relating to your search »
Related posts:
1. Drupal: Sending an Email using the drupal_mail function Here’s a code snippet
that shows you how to use the drupal_mail function to send an email. First,
define a...
2. PHP Function to Validate Credit Card Numbers Here is a very simple credit card
number validation function for your PHP needs. This will simply check if the...
3. Drupal: Create User Accounts Through the Webform Module There are various
ways to create user accounts through Drupal, but if you need to integrate it with
a Webform...
4. SiONSoundObject Quartet – Will Make Anyone Musically-Inclined This Flash will
let you create techno/house/ambient/trance/electronica music at your will.
Unfortunately it only has five chords to play. But...
5. Create multiple-step forms using CCK Fieldgroups in Drupal Lately I’ve been
working on a Drupal project, and one aspect in our priority list is the user-
friendliness for the...
6. Drupal 6: How to Embed a View Into a Template For Drupal: If you need to
embed a view inside a template file, a few lines of code is what...
7. How To Completely Backup your GoDaddy Website Files Godaddy mainly offers
domain names for sale, but they are also selling hosting plans including shared
hosting, VPS and dedicated...
8. 7 Best Temporary Email Services Temporary email services are those that allow
you to create temporary emails you can use for signing up to websites...
1. Jay says:
December 4, 2010 at 4:56 pm
i m using the javascript validator in that the custom script is validating the password
and passwordverification field is same or not but its not displaying the error message
thats my problem
Reply
Top of Form
Name (required)
Mail (required)
Website
Submit
Bottom of Form
○ Blogger Hacks
○ Blogging
○ Computer Tips
○ Contests
○ Daddy Things
○ Drupal
○ Extras
○ Family
○ Firefox
○ Food
○ Funny
○ Games
○ Health
○ HTML5
○ Internet
○ Jian's Milestones
○ Make Money Online
○ Mobile Pinoy
○ Movies
○ Music
○ PHP
○ Prestashop
○ Productivity
○ Reviews
○ Search Engine Optimization
○ Server Administration
○ Sports
○ Top 10's Project
○ Travel
○ Uncategorized
○ Web Design
○ Web Design Resources
○ Web Design Tutorials
○ Web Development
○ Windows 7
○ Wordpress
○ Work
bloggers event browsing compaqpresario cq40 laptop css techniques dreamhost drupal modules drupal
optimization drupalthemeing emails file extensions firefox add-ons firefox extensions first
post goals GodaddyVPSgoldilocks grid based layouts mafia wars cheats MCRYPTmysql new planet online
image editorspersonalization php preferences prestashop hacks productivity social networks spam tab
managementtaskbar temporary email todo list tools twitter user interface wasp-17 watermark Web
Design Web Development Windows 7 wordpress as cms wordpress plugins wordpress upgrade xampp
○ December 2010
○ November 2010
○ October 2010
○ September 2010
○ August 2010
○ July 2010
○ April 2010
○ March 2010
○ February 2010
○ January 2010
○ December 2009
○ November 2009
○ October 2009
○ September 2009
○ August 2009
○ July 2009
○ June 2009
○ May 2009
○ April 2009
○ March 2009
○ February 2009
○ January 2009
○ November 2008
○ October 2008
○ September 2008
•
• About
• Contact
• Portfolio
© 2009 Web Design Philippines | MikeWagan.net. All rights reserved.
Theme by SmashingMagazine and Slimmity