471,066 Members | 1,273 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

Validate multiple form fields with one method

Greetings,

I have a form with a telephone field. It is very specific, as it has
four text boxes - the country code, area code, prefix and suffix. I can
validate each of them individually, but I am stumped as to how to
validate them as a group (as one final validation). I need to check to
see if all (at one time) are filled or empty. The other individual
validations handle cases where the text boxes are filled with letters or
incompletely filled (I simply use a regular expression that checks for
an exact number of integers). However, I need to check if the entire
phone field has been filled or left empty.

One way I did it with PHP was to string all the fields together, then
run an if-else that first checked if the entire string was 12 integers
long, or if it was less than or equal to 2 integers long (the default
country code of 01). If it was the former, then the phone number was OK.
If it was the latter, there was no phone number inputted, and it could
be ignored. And if it was somewhere in-between or failed the regex, the
validation failed and the error message would be displayed.

Is there any method that anyone might suggest to do it as I did it in
PHP? I am hoping to have it done alongside the normal form validation,
so that the

if (Page.IsValid){}

can run as normal and be affected as appropriate from a badly formed
phone number.

TIA
...Geshel
--
************************************************** *******************
My return e-mail address is an automatically monitored spam honeypot.
Do not send e-mail there unless you wish to be reported as a spammer.
Please send all e-mail to my first name at my last name dot org, with
a subject-line of “NEWSGROUP REPLY FOR NEO GESHEL” (all uppercase).
************************************************** *******************
Feb 26 '07 #1
2 2281
On Feb 26, 9:27 am, Neo Geshel <got...@geshel.orgwrote:
Greetings,

I have a form with a telephone field. It is very specific, as it has
four text boxes - the country code, area code, prefix and suffix. I can
validate each of them individually, but I am stumped as to how to
validate them as a group (as one final validation). I need to check to
see if all (at one time) are filled or empty. The other individual
validations handle cases where the text boxes are filled with letters or
incompletely filled (I simply use a regular expression that checks for
an exact number of integers). However, I need to check if the entire
phone field has been filled or left empty.

One way I did it with PHP was to string all the fields together, then
run an if-else that first checked if the entire string was 12 integers
long, or if it was less than or equal to 2 integers long (the default
country code of 01). If it was the former, then the phone number was OK.
If it was the latter, there was no phone number inputted, and it could
be ignored. And if it was somewhere in-between or failed the regex, the
validation failed and the error message would be displayed.

Is there any method that anyone might suggest to do it as I did it in
PHP? I am hoping to have it done alongside the normal form validation,
so that the

if (Page.IsValid){}

can run as normal and be affected as appropriate from a badly formed
phone number.

TIA
...Geshel
--
************************************************** *******************
My return e-mail address is an automatically monitored spam honeypot.
Do not send e-mail there unless you wish to be reported as a spammer.
Please send all e-mail to my first name at my last name dot org, with
a subject-line of "NEWSGROUP REPLY FOR NEO GESHEL" (all uppercase).
************************************************** *******************
1) Using standard validation controls you can perform it with
individual RegularExpressionValidators

e.g.

Private function ValidateFormValues()

CountryCode = Request("CountryCode")
AreaCode = Request("AreaCode")
Prefix = Request("Prefix")
Suffix = Request("Suffix")

strValidationMessage = strValidationMessage &
RegularExpressionValidator(CountryCode, "CountryCode", "[0-9]{2}")
strValidationMessage = strValidationMessage &
RegularExpressionValidator(AreaCode, "AreaCode", "[0-9]{4}")
strValidationMessage = strValidationMessage &
RegularExpressionValidator(Prefix, "Prefix", "[0-9]{4}")
strValidationMessage = strValidationMessage &
RegularExpressionValidator(Suffix, "Suffix", "[0-9]{3}")

' Check to see if errors occurred.
if len( strValidationMessage ) < 1 then
ValidateFormValues = True
else
ValidateFormValues = False
end if

end function

2) using custom code

CountryCode = Request("CountryCode")
AreaCode = Request("AreaCode")
Prefix = Request("Prefix")
Suffix = Request("Suffix")

Phone = CountryCode & AreaCode & Prefix & Suffix

if Not Regex.IsMatch(Phone,"[0-9]{2}[0-9]{4}[0-9]{4}[0-9]
{3}",RegexOptions.IgnoreCase) then
.....

Feb 26 '07 #2
Alexey Smirnov wrote:
On Feb 26, 9:27 am, Neo Geshel <got...@geshel.orgwrote:
>Greetings,

I have a form with a telephone field. It is very specific, as it has
four text boxes - the country code, area code, prefix and suffix. I can
validate each of them individually, but I am stumped as to how to
validate them as a group (as one final validation). I need to check to
see if all (at one time) are filled or empty. The other individual
validations handle cases where the text boxes are filled with letters or
incompletely filled (I simply use a regular expression that checks for
an exact number of integers). However, I need to check if the entire
phone field has been filled or left empty.

One way I did it with PHP was to string all the fields together, then
run an if-else that first checked if the entire string was 12 integers
long, or if it was less than or equal to 2 integers long (the default
country code of 01). If it was the former, then the phone number was OK.
If it was the latter, there was no phone number inputted, and it could
be ignored. And if it was somewhere in-between or failed the regex, the
validation failed and the error message would be displayed.

Is there any method that anyone might suggest to do it as I did it in
PHP? I am hoping to have it done alongside the normal form validation,
so that the

if (Page.IsValid){}

can run as normal and be affected as appropriate from a badly formed
phone number.

TIA
...Geshel
--
************************************************* ********************
My return e-mail address is an automatically monitored spam honeypot.
Do not send e-mail there unless you wish to be reported as a spammer.
Please send all e-mail to my first name at my last name dot org, with
a subject-line of "NEWSGROUP REPLY FOR NEO GESHEL" (all uppercase).
************************************************* ********************
1) Using standard validation controls you can perform it with
individual RegularExpressionValidators

e.g.

Private function ValidateFormValues()

CountryCode = Request("CountryCode")
AreaCode = Request("AreaCode")
Prefix = Request("Prefix")
Suffix = Request("Suffix")

strValidationMessage = strValidationMessage &
RegularExpressionValidator(CountryCode, "CountryCode", "[0-9]{2}")
strValidationMessage = strValidationMessage &
RegularExpressionValidator(AreaCode, "AreaCode", "[0-9]{4}")
strValidationMessage = strValidationMessage &
RegularExpressionValidator(Prefix, "Prefix", "[0-9]{4}")
strValidationMessage = strValidationMessage &
RegularExpressionValidator(Suffix, "Suffix", "[0-9]{3}")

' Check to see if errors occurred.
if len( strValidationMessage ) < 1 then
ValidateFormValues = True
else
ValidateFormValues = False
end if

end function

2) using custom code

CountryCode = Request("CountryCode")
AreaCode = Request("AreaCode")
Prefix = Request("Prefix")
Suffix = Request("Suffix")

Phone = CountryCode & AreaCode & Prefix & Suffix

if Not Regex.IsMatch(Phone,"[0-9]{2}[0-9]{4}[0-9]{4}[0-9]
{3}",RegexOptions.IgnoreCase) then
....
Interesting... and I assume this would go within the button_onclick, but
before the Is_Valid?

If so, this would be a great help. Thanks!! I was just curious, though,
how would I attach a warning message to the message area? I am not
looking to make the fourth validation (the all-fields-or-nothing) cause
the send mail process fail, I just want to throw a warning that if they
proceed, the phone number will not be processed.

Cheers!
...Geshel
--
************************************************** *********************
My return e-mail address is an automatically monitored spam honeypot.
Do not send e-mail there unless you wish to be reported as a spammer.
Please send all e-mail to my first name at my last name dot org, with
a subject-line of “NEWSGROUP REPLY FOR NEO GESHEL” (alluppercase).
************************************************** *********************
Feb 28 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by dskillingstad | last post: by
11 posts views Thread by wolf | last post: by
3 posts views Thread by Martin | last post: by
12 posts views Thread by shank | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.