Hollstar
Member
Posts: 11
Registered: 4/4/2008
Location: Australia
Member Is Offline
|
| posted on 2/28/2009 at 01:23 AM |
|
|
Working around several phone fields
In short, I have four phone fields [Home, Mobile, Business & Fax].
How can I ensure at least one of the fields has been completed and if any additional fields have been completed, that they "pass" the error check
required for such a data type?
|
|
|
Hollstar
Member
Posts: 11
Registered: 4/4/2008
Location: Australia
Member Is Offline
|
| posted on 3/1/2009 at 01:45 AM |
|
|
I've developed the following solution for my problem:
From what I can see, their is no way to ensure that at least one of the phone fields contains data without forcing the end user to enter data into
every phone related field.
01. In the default constructor, copy and paste the following code:
'HiddenPhoneTotal':{'l':'Phone','f':'unsigned','r':true,'mn':1,'mx':4,'t':'t_Phone'}
You will also need to create the following hidden field within your form: <input type="hidden" name="HiddenPhoneTotal" id="hiddenField"
value="">
02. Copy and Paste the following code into the page in question:
<script>
// Developed By: Scott Holland - scott.holland[AT]gmail.com
// 1st March 2009 - Version: 1.0.0
function setPhoneErrorCheck(strFieldName, strIDName, strTitle) {
if (document.frmFormName.elements[strFieldName].value.length > 0) {
// Add each new form element to the SoftComplex Tigra Form Validator a_fields array:
a_fields[escape(strFieldName)] = {'l':strTitle,'r':true,'t':escape(strIDName),'mn':8,'mx':10};
// Add one (1) to the "HiddenPhoneTotal" field. This will ensure an error is not
// trigged because the min value is one (1) as set in the constructor.
document.frmFormName.HiddenPhoneTotal.value ++;
}
else if (document.frmFormName.elements[strFieldName].value.length == 0) {
// Delete the form element from the array should the length of it's value be equal to zero (0)
delete a_fields[escape(strFieldName)];
// Deduct one (1) from "HiddenPhoneTotal". If the value is equal to zero (0), change the value
// to represent a NULL value in order to ensure the error is reported. If the value is not
// change from zero (0) to a NULL, the default error catcher will not fire.
document.frmFormName.HiddenPhoneTotal.value --;
if (document.frmFormName.elements['HiddenPhoneTotal'].value == "0") {
document.frmFormName.elements['HiddenPhoneTotal'].value = "";
}
}
}
</script>
03. Configure each text field like the following example:
<input name="txtHome" id="t_Home" type="text" class="ctrl" style="width: auto;" size="10" maxlength="8"
onBlur="setPhoneErrorCheck(this.name, this.id, 'Home Phone');"/>
04. Change frmFormName in the script above to the name of your form.
You'll still need to assign it a name, but also an ID field. This MUST be the same as the ID of <TD> field for the form element itself. The
reason being, this information is used to set the font color to RED when an error has occured. You'll also notice we have to add an onBlur event. The
only thing you need to change here is "Home Phone". Change this to represent the type of number in question.
In a nutshell, that's it - It's all fairly easy.
The only issue you might face is the min and max value is the same for all of the phone fields. In Australia, that isn't really an issue as almost
all numbers landline or mobile (cell) are between 8 to 10 digits. Toll free numbers are also 10 digits although we do have some businesses who use a 6
digit number. In my eyes, the way around this would be a series of switch statements checking the value of strFieldName or the like. This way, you
could code specific error catches around certain types of numbers.
Hope this helps other people! :)
|
|
|
|