validation strategy?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to insure that at least one of three phone number fields has a value
(requiredfield) but I'm not sure of a way to implement this without server
side logic. Is there a way to use the validation controls to do this?

Thanks.
 
I am not sure if there is a way to do that using the validation controls
other than using the CustomValidator Control. The CustomValidator Control
does require you to write server-side validation code, but if you write a
client-side JavaScript function as well you can set the
ClientValidationFunction property to allow JavaScript enabled browsers to
perform the validation client-side. I have never used this property myself,
but it doesn't sound like it should be too hard. Good Luck!
 
<table>
<tr>
<td>
<asp:DropDownList ID="ddlPhoneType" runat="server">
<asp:ListItem Value="Phone">Phone</asp:ListItem>
<asp:ListItem Value="Mobile">Mobile</asp:ListItem>
<asp:ListItem Value="Fax">Fax</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="txtMandatoryPhone"
runat="server"></asp:TextBox><font color="red">*</font>
<asp:RequiredFieldValidator ID="valMandatoryPhone"
runat="Server" ControlToValidate="txtMandatoryPhone"
ErrorMessage="You must enter at least one contact
information"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="Phone">Phone</asp:ListItem>
<asp:ListItem Value="Mobile">Mobile</asp:ListItem>
<asp:ListItem Value="Fax">Fax</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="Phone">Phone</asp:ListItem>
<asp:ListItem Value="Mobile">Mobile</asp:ListItem>
<asp:ListItem Value="Fax">Fax</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
</table>
 
Clever workaround Phillip!

but I'm looking for a way to just insure that at least one of three
textboxes has a value in it without designating on as the mandatory field, as
these are in different sections of the form - e.g. work fields vs home fields.

I have a server side validation working now, but since it doesn't use
validation controls the error message doesn't disappear once the user tabs
out of the field.

I guess I'm going to have to brush up on my javascript!

Thanks for your suggestion.
 
Thanks Nathan, I think I will try the CustomValidator, I just need to brush
up on my javascript to access the values of the other controls.

There should be an intrinsic pattern to edit one control against multiple
other controls (MultiCompareValidator ?).
 
You can also have each of those textboxes copying their value to a hidden
textbox by handling the client-side event named "onchange", like this:

<asp:TextBox ID="txtHomePhone" runat="server"
onchange="copyvalue();"></asp:TextBox>
<asp:TextBox ID="txtBusPhone" runat="server"
onchange="copyvalue();"></asp:TextBox>
<asp:TextBox ID="txtMobile" runat="server"
onchange="copyvalue();"></asp:TextBox>

At the end of your webform add a hidden textbox with validation controls
(for both required field and valid phone entry):

<asp:TextBox ID="txtPhone" Runat="server" CssClass="Hidden"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="txtPhone" Runat="server"
ErrorMessage="You must enter at least one phone
number"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ControlToValidate ="txtPhone" Runat="server"
ErrorMessage="Not a vaild phone nubmer"
Display="Dynamic"
ValidationExpression="^\(?\d{3}\)?\s?\d{3}[-\s]?\d{4}$"></asp:RegularExpressionValidator>

Then in the javascript script section of your code:
function copyvalue()
{
var txtSrc = window.event.srcElement;
var txtTarget = document.getElementById ("txtPhone");
if (txtTarget!=null)
{
txtTarget.value=txtSrc.value;
}
}

Of course make sure your stylesheet has the .hidden style defined, e.g.
..Hidden {display:none;}
 
Even more clever!

The only fault I see in this is that I can't mark the missing fields
visually, right now I use validators to put exclamation point gif next to
missing fields and error text in a validation summary. so for the time being
I'm not using a validator but rejecting the page if server side method
determines all phone fields are blank and setting visible label fields with
image and error text.

Now I'm thinking it would be cool if I could perform the tests server side,
then trigger validation controls from server code, so the validation errors
would appear in the validation summary.

Sorry, guess I'm want it just so ;)

Thanks for a classy suggestion though, I might wind up having to reduce
expectations and use it ;)

Phillip Williams said:
You can also have each of those textboxes copying their value to a hidden
textbox by handling the client-side event named "onchange", like this:

<asp:TextBox ID="txtHomePhone" runat="server"
onchange="copyvalue();"></asp:TextBox>
<asp:TextBox ID="txtBusPhone" runat="server"
onchange="copyvalue();"></asp:TextBox>
<asp:TextBox ID="txtMobile" runat="server"
onchange="copyvalue();"></asp:TextBox>

At the end of your webform add a hidden textbox with validation controls
(for both required field and valid phone entry):

<asp:TextBox ID="txtPhone" Runat="server" CssClass="Hidden"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="txtPhone" Runat="server"
ErrorMessage="You must enter at least one phone
number"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ControlToValidate ="txtPhone" Runat="server"
ErrorMessage="Not a vaild phone nubmer"
Display="Dynamic"
ValidationExpression="^\(?\d{3}\)?\s?\d{3}[-\s]?\d{4}$"></asp:RegularExpressionValidator>

Then in the javascript script section of your code:
function copyvalue()
{
var txtSrc = window.event.srcElement;
var txtTarget = document.getElementById ("txtPhone");
if (txtTarget!=null)
{
txtTarget.value=txtSrc.value;
}
}

Of course make sure your stylesheet has the .hidden style defined, e.g.
.Hidden {display:none;}
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Dabbler said:
Clever workaround Phillip!

but I'm looking for a way to just insure that at least one of three
textboxes has a value in it without designating on as the mandatory field, as
these are in different sections of the form - e.g. work fields vs home fields.

I have a server side validation working now, but since it doesn't use
validation controls the error message doesn't disappear once the user tabs
out of the field.

I guess I'm going to have to brush up on my javascript!

Thanks for your suggestion.
 
The custom server control that you will build has to have a strategy to
validate the entries using javascript. This is what we are doing here. In
other words, this discussion is not irrelevant. You can package the
javascript skills that you might learn from this discussion into your custom
validator's client-side script and add to it the portion that you already
developed (server-side validation).

As for the *only fault* that you identified (can't mark the missing fields
visually), I modified the code slightly to do that and placed it as a demo on
this link:
http://www.webswapp.com/codesamples/aspnet20/textbox/demo2.aspx

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Dabbler said:
Even more clever!

The only fault I see in this is that I can't mark the missing fields
visually, right now I use validators to put exclamation point gif next to
missing fields and error text in a validation summary. so for the time being
I'm not using a validator but rejecting the page if server side method
determines all phone fields are blank and setting visible label fields with
image and error text.

Now I'm thinking it would be cool if I could perform the tests server side,
then trigger validation controls from server code, so the validation errors
would appear in the validation summary.

Sorry, guess I'm want it just so ;)

Thanks for a classy suggestion though, I might wind up having to reduce
expectations and use it ;)

Phillip Williams said:
You can also have each of those textboxes copying their value to a hidden
textbox by handling the client-side event named "onchange", like this:

<asp:TextBox ID="txtHomePhone" runat="server"
onchange="copyvalue();"></asp:TextBox>
<asp:TextBox ID="txtBusPhone" runat="server"
onchange="copyvalue();"></asp:TextBox>
<asp:TextBox ID="txtMobile" runat="server"
onchange="copyvalue();"></asp:TextBox>

At the end of your webform add a hidden textbox with validation controls
(for both required field and valid phone entry):

<asp:TextBox ID="txtPhone" Runat="server" CssClass="Hidden"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="txtPhone" Runat="server"
ErrorMessage="You must enter at least one phone
number"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ControlToValidate ="txtPhone" Runat="server"
ErrorMessage="Not a vaild phone nubmer"
Display="Dynamic"
ValidationExpression="^\(?\d{3}\)?\s?\d{3}[-\s]?\d{4}$"></asp:RegularExpressionValidator>

Then in the javascript script section of your code:
function copyvalue()
{
var txtSrc = window.event.srcElement;
var txtTarget = document.getElementById ("txtPhone");
if (txtTarget!=null)
{
txtTarget.value=txtSrc.value;
}
}

Of course make sure your stylesheet has the .hidden style defined, e.g.
.Hidden {display:none;}
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Dabbler said:
Clever workaround Phillip!

but I'm looking for a way to just insure that at least one of three
textboxes has a value in it without designating on as the mandatory field, as
these are in different sections of the form - e.g. work fields vs home fields.

I have a server side validation working now, but since it doesn't use
validation controls the error message doesn't disappear once the user tabs
out of the field.

I guess I'm going to have to brush up on my javascript!

Thanks for your suggestion.

:

<table>
<tr>
<td>
<asp:DropDownList ID="ddlPhoneType" runat="server">
<asp:ListItem Value="Phone">Phone</asp:ListItem>
<asp:ListItem Value="Mobile">Mobile</asp:ListItem>
<asp:ListItem Value="Fax">Fax</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="txtMandatoryPhone"
runat="server"></asp:TextBox><font color="red">*</font>
<asp:RequiredFieldValidator ID="valMandatoryPhone"
runat="Server" ControlToValidate="txtMandatoryPhone"
ErrorMessage="You must enter at least one contact
information"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="Phone">Phone</asp:ListItem>
<asp:ListItem Value="Mobile">Mobile</asp:ListItem>
<asp:ListItem Value="Fax">Fax</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="Phone">Phone</asp:ListItem>
<asp:ListItem Value="Mobile">Mobile</asp:ListItem>
<asp:ListItem Value="Fax">Fax</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
</table>
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:

I need to insure that at least one of three phone number fields has a value
(requiredfield) but I'm not sure of a way to implement this without server
side logic. Is there a way to use the validation controls to do this?

Thanks.
 
Thanks Philip, nice demo! But I think the source code applies to a different
demo?


Phillip Williams said:
The custom server control that you will build has to have a strategy to
validate the entries using javascript. This is what we are doing here. In
other words, this discussion is not irrelevant. You can package the
javascript skills that you might learn from this discussion into your custom
validator's client-side script and add to it the portion that you already
developed (server-side validation).

As for the *only fault* that you identified (can't mark the missing fields
visually), I modified the code slightly to do that and placed it as a demo on
this link:
http://www.webswapp.com/codesamples/aspnet20/textbox/demo2.aspx

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Dabbler said:
Even more clever!

The only fault I see in this is that I can't mark the missing fields
visually, right now I use validators to put exclamation point gif next to
missing fields and error text in a validation summary. so for the time being
I'm not using a validator but rejecting the page if server side method
determines all phone fields are blank and setting visible label fields with
image and error text.

Now I'm thinking it would be cool if I could perform the tests server side,
then trigger validation controls from server code, so the validation errors
would appear in the validation summary.

Sorry, guess I'm want it just so ;)

Thanks for a classy suggestion though, I might wind up having to reduce
expectations and use it ;)

Phillip Williams said:
You can also have each of those textboxes copying their value to a hidden
textbox by handling the client-side event named "onchange", like this:

<asp:TextBox ID="txtHomePhone" runat="server"
onchange="copyvalue();"></asp:TextBox>
<asp:TextBox ID="txtBusPhone" runat="server"
onchange="copyvalue();"></asp:TextBox>
<asp:TextBox ID="txtMobile" runat="server"
onchange="copyvalue();"></asp:TextBox>

At the end of your webform add a hidden textbox with validation controls
(for both required field and valid phone entry):

<asp:TextBox ID="txtPhone" Runat="server" CssClass="Hidden"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="txtPhone" Runat="server"
ErrorMessage="You must enter at least one phone
number"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ControlToValidate ="txtPhone" Runat="server"
ErrorMessage="Not a vaild phone nubmer"
Display="Dynamic"
ValidationExpression="^\(?\d{3}\)?\s?\d{3}[-\s]?\d{4}$"></asp:RegularExpressionValidator>

Then in the javascript script section of your code:
function copyvalue()
{
var txtSrc = window.event.srcElement;
var txtTarget = document.getElementById ("txtPhone");
if (txtTarget!=null)
{
txtTarget.value=txtSrc.value;
}
}

Of course make sure your stylesheet has the .hidden style defined, e.g.
.Hidden {display:none;}
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:

Clever workaround Phillip!

but I'm looking for a way to just insure that at least one of three
textboxes has a value in it without designating on as the mandatory field, as
these are in different sections of the form - e.g. work fields vs home fields.

I have a server side validation working now, but since it doesn't use
validation controls the error message doesn't disappear once the user tabs
out of the field.

I guess I'm going to have to brush up on my javascript!

Thanks for your suggestion.

:

<table>
<tr>
<td>
<asp:DropDownList ID="ddlPhoneType" runat="server">
<asp:ListItem Value="Phone">Phone</asp:ListItem>
<asp:ListItem Value="Mobile">Mobile</asp:ListItem>
<asp:ListItem Value="Fax">Fax</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="txtMandatoryPhone"
runat="server"></asp:TextBox><font color="red">*</font>
<asp:RequiredFieldValidator ID="valMandatoryPhone"
runat="Server" ControlToValidate="txtMandatoryPhone"
ErrorMessage="You must enter at least one contact
information"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="Phone">Phone</asp:ListItem>
<asp:ListItem Value="Mobile">Mobile</asp:ListItem>
<asp:ListItem Value="Fax">Fax</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="Phone">Phone</asp:ListItem>
<asp:ListItem Value="Mobile">Mobile</asp:ListItem>
<asp:ListItem Value="Fax">Fax</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
</table>
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:

I need to insure that at least one of three phone number fields has a value
(requiredfield) but I'm not sure of a way to implement this without server
side logic. Is there a way to use the validation controls to do this?

Thanks.
 
Are you sure you clicked on the right tab for the source code? There are 2
demos on the same section. The source code can be reached directly through
this link:
http://www.webswapp.com/codesamples/viewSource.aspx?file=~/codesamples/aspnet20/textbox/demo2.aspx

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Dabbler said:
Thanks Philip, nice demo! But I think the source code applies to a different
demo?


Phillip Williams said:
The custom server control that you will build has to have a strategy to
validate the entries using javascript. This is what we are doing here. In
other words, this discussion is not irrelevant. You can package the
javascript skills that you might learn from this discussion into your custom
validator's client-side script and add to it the portion that you already
developed (server-side validation).

As for the *only fault* that you identified (can't mark the missing fields
visually), I modified the code slightly to do that and placed it as a demo on
this link:
http://www.webswapp.com/codesamples/aspnet20/textbox/demo2.aspx

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Dabbler said:
Even more clever!

The only fault I see in this is that I can't mark the missing fields
visually, right now I use validators to put exclamation point gif next to
missing fields and error text in a validation summary. so for the time being
I'm not using a validator but rejecting the page if server side method
determines all phone fields are blank and setting visible label fields with
image and error text.

Now I'm thinking it would be cool if I could perform the tests server side,
then trigger validation controls from server code, so the validation errors
would appear in the validation summary.

Sorry, guess I'm want it just so ;)

Thanks for a classy suggestion though, I might wind up having to reduce
expectations and use it ;)

:

You can also have each of those textboxes copying their value to a hidden
textbox by handling the client-side event named "onchange", like this:

<asp:TextBox ID="txtHomePhone" runat="server"
onchange="copyvalue();"></asp:TextBox>
<asp:TextBox ID="txtBusPhone" runat="server"
onchange="copyvalue();"></asp:TextBox>
<asp:TextBox ID="txtMobile" runat="server"
onchange="copyvalue();"></asp:TextBox>

At the end of your webform add a hidden textbox with validation controls
(for both required field and valid phone entry):

<asp:TextBox ID="txtPhone" Runat="server" CssClass="Hidden"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="txtPhone" Runat="server"
ErrorMessage="You must enter at least one phone
number"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ControlToValidate ="txtPhone" Runat="server"
ErrorMessage="Not a vaild phone nubmer"
Display="Dynamic"
ValidationExpression="^\(?\d{3}\)?\s?\d{3}[-\s]?\d{4}$"></asp:RegularExpressionValidator>

Then in the javascript script section of your code:
function copyvalue()
{
var txtSrc = window.event.srcElement;
var txtTarget = document.getElementById ("txtPhone");
if (txtTarget!=null)
{
txtTarget.value=txtSrc.value;
}
}

Of course make sure your stylesheet has the .hidden style defined, e.g.
.Hidden {display:none;}
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:

Clever workaround Phillip!

but I'm looking for a way to just insure that at least one of three
textboxes has a value in it without designating on as the mandatory field, as
these are in different sections of the form - e.g. work fields vs home fields.

I have a server side validation working now, but since it doesn't use
validation controls the error message doesn't disappear once the user tabs
out of the field.

I guess I'm going to have to brush up on my javascript!

Thanks for your suggestion.

:

<table>
<tr>
<td>
<asp:DropDownList ID="ddlPhoneType" runat="server">
<asp:ListItem Value="Phone">Phone</asp:ListItem>
<asp:ListItem Value="Mobile">Mobile</asp:ListItem>
<asp:ListItem Value="Fax">Fax</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="txtMandatoryPhone"
runat="server"></asp:TextBox><font color="red">*</font>
<asp:RequiredFieldValidator ID="valMandatoryPhone"
runat="Server" ControlToValidate="txtMandatoryPhone"
ErrorMessage="You must enter at least one contact
information"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="Phone">Phone</asp:ListItem>
<asp:ListItem Value="Mobile">Mobile</asp:ListItem>
<asp:ListItem Value="Fax">Fax</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="Phone">Phone</asp:ListItem>
<asp:ListItem Value="Mobile">Mobile</asp:ListItem>
<asp:ListItem Value="Fax">Fax</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
</table>
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:

I need to insure that at least one of three phone number fields has a value
(requiredfield) but I'm not sure of a way to implement this without server
side logic. Is there a way to use the validation controls to do this?

Thanks.
 
right you are, I clicked the C# tab by instinct, as I lost interest in VB way
before it became a typed language.

I found your findcontrol function interesting, why do you use that as
opposed to document.getElementById? is it because you might give the same id
to two element types?

Thanks again.

Phillip Williams said:
Are you sure you clicked on the right tab for the source code? There are 2
demos on the same section. The source code can be reached directly through
this link:
http://www.webswapp.com/codesamples/viewSource.aspx?file=~/codesamples/aspnet20/textbox/demo2.aspx

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Dabbler said:
Thanks Philip, nice demo! But I think the source code applies to a different
demo?


Phillip Williams said:
The custom server control that you will build has to have a strategy to
validate the entries using javascript. This is what we are doing here. In
other words, this discussion is not irrelevant. You can package the
javascript skills that you might learn from this discussion into your custom
validator's client-side script and add to it the portion that you already
developed (server-side validation).

As for the *only fault* that you identified (can't mark the missing fields
visually), I modified the code slightly to do that and placed it as a demo on
this link:
http://www.webswapp.com/codesamples/aspnet20/textbox/demo2.aspx

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:


Even more clever!

The only fault I see in this is that I can't mark the missing fields
visually, right now I use validators to put exclamation point gif next to
missing fields and error text in a validation summary. so for the time being
I'm not using a validator but rejecting the page if server side method
determines all phone fields are blank and setting visible label fields with
image and error text.

Now I'm thinking it would be cool if I could perform the tests server side,
then trigger validation controls from server code, so the validation errors
would appear in the validation summary.

Sorry, guess I'm want it just so ;)

Thanks for a classy suggestion though, I might wind up having to reduce
expectations and use it ;)

:

You can also have each of those textboxes copying their value to a hidden
textbox by handling the client-side event named "onchange", like this:

<asp:TextBox ID="txtHomePhone" runat="server"
onchange="copyvalue();"></asp:TextBox>
<asp:TextBox ID="txtBusPhone" runat="server"
onchange="copyvalue();"></asp:TextBox>
<asp:TextBox ID="txtMobile" runat="server"
onchange="copyvalue();"></asp:TextBox>

At the end of your webform add a hidden textbox with validation controls
(for both required field and valid phone entry):

<asp:TextBox ID="txtPhone" Runat="server" CssClass="Hidden"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="txtPhone" Runat="server"
ErrorMessage="You must enter at least one phone
number"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ControlToValidate ="txtPhone" Runat="server"
ErrorMessage="Not a vaild phone nubmer"
Display="Dynamic"
ValidationExpression="^\(?\d{3}\)?\s?\d{3}[-\s]?\d{4}$"></asp:RegularExpressionValidator>

Then in the javascript script section of your code:
function copyvalue()
{
var txtSrc = window.event.srcElement;
var txtTarget = document.getElementById ("txtPhone");
if (txtTarget!=null)
{
txtTarget.value=txtSrc.value;
}
}

Of course make sure your stylesheet has the .hidden style defined, e.g.
.Hidden {display:none;}
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:

Clever workaround Phillip!

but I'm looking for a way to just insure that at least one of three
textboxes has a value in it without designating on as the mandatory field, as
these are in different sections of the form - e.g. work fields vs home fields.

I have a server side validation working now, but since it doesn't use
validation controls the error message doesn't disappear once the user tabs
out of the field.

I guess I'm going to have to brush up on my javascript!

Thanks for your suggestion.

:

<table>
<tr>
<td>
<asp:DropDownList ID="ddlPhoneType" runat="server">
<asp:ListItem Value="Phone">Phone</asp:ListItem>
<asp:ListItem Value="Mobile">Mobile</asp:ListItem>
<asp:ListItem Value="Fax">Fax</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="txtMandatoryPhone"
runat="server"></asp:TextBox><font color="red">*</font>
<asp:RequiredFieldValidator ID="valMandatoryPhone"
runat="Server" ControlToValidate="txtMandatoryPhone"
ErrorMessage="You must enter at least one contact
information"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="Phone">Phone</asp:ListItem>
<asp:ListItem Value="Mobile">Mobile</asp:ListItem>
<asp:ListItem Value="Fax">Fax</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="Phone">Phone</asp:ListItem>
<asp:ListItem Value="Mobile">Mobile</asp:ListItem>
<asp:ListItem Value="Fax">Fax</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
</table>
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:

I need to insure that at least one of three phone number fields has a value
(requiredfield) but I'm not sure of a way to implement this without server
side logic. Is there a way to use the validation controls to do this?

Thanks.
 
The page you were looking at in this demo is a content page; which means that
any controls on it will have their browser ID prefixed with the
NamingContainer(s) IDs. For example the textbox for entering the date has an
ID that looks like this: ctl00_ContentPlaceHolder1_txtDate1. If I had simply
used document.getElementById("txtDate1") I would have not found it. So my
customized FindControl JavaScript function searches for a control with an ID
that ends with the ID I am looking for.

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Dabbler said:
right you are, I clicked the C# tab by instinct, as I lost interest in VB way
before it became a typed language.

I found your findcontrol function interesting, why do you use that as
opposed to document.getElementById? is it because you might give the same id
to two element types?

Thanks again.

Phillip Williams said:
Are you sure you clicked on the right tab for the source code? There are 2
demos on the same section. The source code can be reached directly through
this link:
http://www.webswapp.com/codesamples/viewSource.aspx?file=~/codesamples/aspnet20/textbox/demo2.aspx

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Dabbler said:
Thanks Philip, nice demo! But I think the source code applies to a different
demo?


:

The custom server control that you will build has to have a strategy to
validate the entries using javascript. This is what we are doing here. In
other words, this discussion is not irrelevant. You can package the
javascript skills that you might learn from this discussion into your custom
validator's client-side script and add to it the portion that you already
developed (server-side validation).

As for the *only fault* that you identified (can't mark the missing fields
visually), I modified the code slightly to do that and placed it as a demo on
this link:
http://www.webswapp.com/codesamples/aspnet20/textbox/demo2.aspx

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:


Even more clever!

The only fault I see in this is that I can't mark the missing fields
visually, right now I use validators to put exclamation point gif next to
missing fields and error text in a validation summary. so for the time being
I'm not using a validator but rejecting the page if server side method
determines all phone fields are blank and setting visible label fields with
image and error text.

Now I'm thinking it would be cool if I could perform the tests server side,
then trigger validation controls from server code, so the validation errors
would appear in the validation summary.

Sorry, guess I'm want it just so ;)

Thanks for a classy suggestion though, I might wind up having to reduce
expectations and use it ;)

:

You can also have each of those textboxes copying their value to a hidden
textbox by handling the client-side event named "onchange", like this:

<asp:TextBox ID="txtHomePhone" runat="server"
onchange="copyvalue();"></asp:TextBox>
<asp:TextBox ID="txtBusPhone" runat="server"
onchange="copyvalue();"></asp:TextBox>
<asp:TextBox ID="txtMobile" runat="server"
onchange="copyvalue();"></asp:TextBox>

At the end of your webform add a hidden textbox with validation controls
(for both required field and valid phone entry):

<asp:TextBox ID="txtPhone" Runat="server" CssClass="Hidden"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="txtPhone" Runat="server"
ErrorMessage="You must enter at least one phone
number"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ControlToValidate ="txtPhone" Runat="server"
ErrorMessage="Not a vaild phone nubmer"
Display="Dynamic"
ValidationExpression="^\(?\d{3}\)?\s?\d{3}[-\s]?\d{4}$"></asp:RegularExpressionValidator>

Then in the javascript script section of your code:
function copyvalue()
{
var txtSrc = window.event.srcElement;
var txtTarget = document.getElementById ("txtPhone");
if (txtTarget!=null)
{
txtTarget.value=txtSrc.value;
}
}

Of course make sure your stylesheet has the .hidden style defined, e.g.
.Hidden {display:none;}
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:

Clever workaround Phillip!

but I'm looking for a way to just insure that at least one of three
textboxes has a value in it without designating on as the mandatory field, as
these are in different sections of the form - e.g. work fields vs home fields.

I have a server side validation working now, but since it doesn't use
validation controls the error message doesn't disappear once the user tabs
out of the field.

I guess I'm going to have to brush up on my javascript!

Thanks for your suggestion.

:

<table>
<tr>
<td>
<asp:DropDownList ID="ddlPhoneType" runat="server">
<asp:ListItem Value="Phone">Phone</asp:ListItem>
<asp:ListItem Value="Mobile">Mobile</asp:ListItem>
<asp:ListItem Value="Fax">Fax</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="txtMandatoryPhone"
runat="server"></asp:TextBox><font color="red">*</font>
<asp:RequiredFieldValidator ID="valMandatoryPhone"
runat="Server" ControlToValidate="txtMandatoryPhone"
ErrorMessage="You must enter at least one contact
information"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="Phone">Phone</asp:ListItem>
<asp:ListItem Value="Mobile">Mobile</asp:ListItem>
<asp:ListItem Value="Fax">Fax</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="Phone">Phone</asp:ListItem>
<asp:ListItem Value="Mobile">Mobile</asp:ListItem>
<asp:ListItem Value="Fax">Fax</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
</table>
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:

I need to insure that at least one of three phone number fields has a value
(requiredfield) but I'm not sure of a way to implement this without server
side logic. Is there a way to use the validation controls to do this?

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top