Category Archives: RequiredFieldValidator

[RESOLVED] Required Field Validators not firing in the Page

Hi All,

I have required field validators in my ASP.NET application form.

In the button click event of the FORM, i.e. OnClientClick(), I am calling a javascript function to validate the controls in this same page. Due to this the required field validators are not getting fired.

Please help me, I need both the javascript function be fired on the button click , as well as required field validators also to work efficiently.

Regards

Jasumon

Hi,

Can u post the sample code. So that we can chk where u went wrong.

Did u set all required properties like validationgroup,causesvalidation....

Thanks

Sree

add Page_ClientValidate() in your javascript function. which will check the server side validation is successful. if it is success then it will execute the code in your function. This problem will always exists because the server side validation controls will also perform validation on client side.

Source code?

 

Morzel

And what is if he using ValidationGroups?

 

Morzel

Hello,

Both type of validation will work, java script function will call on onclientclick event and required field validation will work on click EVENT and also use validation group property like.

<head runat="server">
<script type="text/javascript" language="javascript">
function funadd()
{
alert("hi");
}
</script>
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="txtNmae" runat="server" ValidationGroup="submit"></asp:TextBox><asp:RequiredFieldValidator ID="rfv" runat="server"
    ControlToValidate="txtNmae" ErrorMessage="*" Display="Dynamic" ValidationGroup="submit"></asp:RequiredFieldValidator>
    <asp:Button ID="btn" runat="server" Text=" Submit" ValidationGroup="submit"
            OnClientClick="funadd()" onclick="btn_Click" />
    </div>
    </form>

Hi,

 

Change your javascript function funadd() like below and check it will work.

 

Deepakeffectlabs

Hello,

Both type of validation will work, java script function will call on onclientclick event and required field validation will work on click EVENT and also use validation group property like.

 

<head runat="server">

<script type="text/javascript" language="javascript"> function funadd() {

if(Page_ClientValidate())

{

alert("hi");} } </script> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtNmae" runat="server" ValidationGroup="submit"></asp:TextBox><asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtNmae" ErrorMessage="*" Display="Dynamic" ValidationGroup="submit"></asp:RequiredFieldValidator> <asp:Button ID="btn" runat="server" Text=" Submit" ValidationGroup="submit" OnClientClick="funadd()" onclick="btn_Click" /> </div> </form>

 

 Put in Page_Load Event...

 

Page.GetPostBackEventReference(btnPayBook);

 

In Javascript code do this

<asp:Button runat="server" ID="btnPayBook"  onclick="btnPayBook_Click"
                                                                Text="Book" Width="97px" OnClientClick="doStuff(this);" />
                                                           
                                                            <script type="text/javascript">
                                                                function doStuff(btn)
                                                                {

                                                                    if(Page_ClientValidate()){ 

                                                                    btn.value = 'Processing...';
                                                                 
                                                                    __doPostBack("btnPayBook", "OnClick");

                                                                  }
                                                                }
                                                           
                                                            </script>

//This should help what you require

 

Hi All,

 

I have added the below given code to the javascript function, this resolved the issue. Now the required field validators are firing correctly.

 

if (!Page_ClientValidate())

return false;

 

Regards

Jasumon

 

May be some problem with your Java script function try to call simple alert box from your page.......

[RESOLVED] gridview update not firing with required field validator

I have a gridview that contains a textbox, a drop down, and a checkbox.  The commandfield of the gridview has ShowEditButton="true".  All of the gridview updating takes place in the "_OnRowUpdating" event.  I have a Panel above the gridview with a table and some fields.  Everything in the gridview works perfectly, the edit, the update etc., however, when I add a requiredfieldvalidator to the panel above the gridview, the gridview update functionality no longer works. e.g., clicking the update button does not appear to fire.  If I comment out the requiredfieldvalidator, everything works fine. 

Assign a ValidationGroup to your Validator and whatever Buttons, etc., that are supposed to trigger the validation.

[RESOLVED] Validation fails when fields are populated on page_load

Hi All,

I'm writing a small web application in c# and i'm running into a small problem.

Basically, on page_load() my backend MySQL db is queried to bring back the user data, and that is then inserted into the textbox controls as below:

txtEmailAddress.Text = func.dbString(dr["emailAddress"]);
txtForename.Text = func.dbString(dr["forename"]);

Now the problem occurs when the RequiredFieldValidator kicks in. In my form processing function, I call Page.IsValid(), it fails and displays the required message next to the required fields.

I was wondering if there is a workaround for this, or a better way to go about what i'm trying to do.

Thanks in advance for any help,

Dave

DaveCarter

. In my form processing function, I call Page.IsValid(), it fails and displays the required message next to the required fields.

That is the intended behaviour....if the textbox is empty...then RFV will get fired on Page.IsValid....make sure the textbox is not empty....some where you are empting the textbox....

Thanks.

I know exactly where the field is being emptied. On the page_load.

Basically I've set about the task of re-developing one of my old PHP apps into .NET - In turn, the DB structure has been altered meaning some of the fields are null at this time.

When page_load() is called it runs a db query and just sets the values of the text boxes to whatever comes back from the DB. Now, if these records come back null, then the user fills out their information and hits save. However, because the textboxes are re-populated from the DB on page load, it completely disregards the new information the user has entered.

It seems like such a simple thing! Whenever you go onto a profile page anywhere, all of your fields are filled out and they work as they should. I could do this very easily in PHP, how can I replicate it here?

Dave

You got to use if(!IsPostBack) condition to populate the DB records into the textbox for the first time....then on the consecutive page loads, DB records will not be populated....

Thanks.

Thanks! I'll try this when I get home.

[RESOLVED] Confused how required field validator discriminates by certain items in drop down list.

I am maintaining some Asp.net code. On the web form is a drop down listbox which is populated through an object data source.
The contents of the drop down listbox look as follows, where you have a list of names. The names with dashes next to them
are the names of the captains of a team. The other names are the team members.

The way the code currently works, is that if somoeone selects one of the names with dashes in it, then they get the required field
validator message indicating that an item was not selected. Otherwise if they chose a regular non captain name, the selected item is
processed.

I have a new requirement which requires me to modify the code so that the captain names can be selected, without getting the validation
error. I am confused because I don't see how the required field validator code can discriminate between drop down items with dashes next
to them and the regular names without dashes. If I knew that, then I could allow for selecting one of the captain names. Can someone help me
out? At the bottom of this post is my Aspx code.

  --Select Item---
  Bob
  Jeff
  Mike
  --Jim---
  Amy
  Jeff
  Diane
  --Mora--
  Jimmy
  Mary
  Kerry

 

 


</tr>
        <tr>
        <td>SMT Group</td>
        <td><img alt="Required" src="/Images/arrow.gif" /></td>
        <td>
        <asp:DropDownList ID="dropDownListArea" runat="server">
        <asp:ListItem text="-select one-" value="" />
        </asp:DropDownList>
        <asp:RequiredFieldValidator id="requiredFieldValidator15" runat="server" controltovalidate="dropDownListArea" errormessage="Area is required."
        initialvalue="" setfocusonerror="true" />
        </td>
</tr>

 

In requiredfieldvalidator tag, u can see initialvalue property set to blank string "" that means any item in dropdownlist with valuefield as blank can not be selected and requiredfieldvalidator will give error message...

u can check code which binds items(data) to dropdownlist (may be in codebehind or in .aspx if static data) and verify that if captain name (with dash)

then valuefield may have set to "" and that is where u need to make change, either remove dash from those names or add condition that only first item will have valuefield as "" i.e. --select item--

hope this helps...

See in requirefieldvalidator (rfv) the initialValue is set to ''. It can be possible that your query returns the captain names (items with dashes) with blank ('') datavaluefield unlike team meber items. Do these 2 things:

1. Try changing a value property of item ---Select One--- to 0 (right now it is ''), and the same in rfv (InitialValue=0)

2. If the above does not solve ur problem then you must post here query/code logic used to bind dropdownlist.

InitialValue property is specify a default value other than empty string.so use it when you trying to check in dropdownlist...

Hi guys, thank you all for your help!
I think the validation is enforced in the C# code from what I can tell. See the method below named
"BuildTeamList". I beleive the color of the captain names and other team member names is occuring
on lines 13 and 16. Yet I played with the code and still can't get the code to accept my selection
of a team captain name. If you guys could help me out more, I would really appreciate it.


// init the Team Team dropdown list
void PopulateList()
{
    CommonWeb.BuildTeamList(dropDownListTeam);

}

 


1 public BuildTeamList(ListControl listControl)
2 {
3  if (listControl == null) return;
4  string TeamGroup = string.Empty;
5  string htmlSpace = HttpContext.Current.Server.HtmlDecode("&nbsp;");
6
7  DataTable dtTeams = ListTeams();
8  foreach( DataRow dr in dtTeams();
9   {
10      // add Team group
11      if(!dr["TeamGroup"].Equals(TeamGroup))
12       {
13 List listItemTeamGroup = new ListItem(string.Format("{0}", dr["TeamGroup"]));
14
15
16  listItemTeamGroup.Attributes.Add("class","itemHighlighted");
17 listControl.Items.Add(listItemTeamGroup);
18  TeamGroup = (string)dr["TeamGroup"];
19
20        }
21       // add Team
22    ListItem listItemTeam = new ListItem(string.Format("{0}{0}{0}{1}",htmlSpace, dr["TeamName"]),
23    (string)dr[TeamId]);
24   
25    listControl.Items.Add(listItemTeam);
26}
27}

try changing the code as

 11      if(!dr["TeamGroup"].Equals(TeamGroup))
12       {
13 List listItemTeamGroup = new ListItem(string.Format("{0}", (string)dr["TeamId"]));

hope this helps...

Works great, thanks so much!