Solved

Need JQuery Function to check if field is Null in ASP.NET (SharePoint)

Posted on 2012-03-30
8
426 Views
Last Modified: 2012-04-05
I have a form that I am building in SharePoint Designer 2007 that has several fields that are required depending on the value of a specific field, so I need to create my own required field indicator. For example

when field is empty:
label     Field1 *

when field has a value:
label     Field1
 
I would like to have an '*' next to an empty required field and have the '*' be hidden when the field(s) has a value.

Another problem I am having is that when I use the <SharePoint:FormField ..../> tag with a <P>*</P>.   The '*' is put on another line instead of next to my field.
0
Comment
Question by:macarrillo1
  • 5
  • 3
8 Comments
 
LVL 51

Expert Comment

by:tedbilly
Comment Utility
There are client-side validation controls available in ASP.NET that will do what you require.  They generate the client side JavaScript & also validate the forms server-side to do things like prevent SQL injection attacks.  Why are you not using them?
0
 
LVL 9

Author Comment

by:macarrillo1
Comment Utility
I need to build a form that has required fields that are dependent on the value of a field as follows:

Field 1 (Required)*
Field 2 (not Required)

Field 3 (If Field 1 = A then Required and visible else Not required and Hidden)*
Field 4 (if Field 1 = B Then Required and visible else Not required and Hidden)*

I want the '*' to appear next to required fields and disappear when the field has data.
so far I have a function that is not working ( I know I am using the click event instead of the change event to accelerate testing) as follows:

$(document).ready(function()    {
     $("input[id*=ff5]").click(function()    {
         $("div#ff5".hide();
         });
});

This is within a <Script type="text/javascript"> tag and of course include the Script with the jquery-1.7.2.js src.

I used the input tag because I can't directly reference SharePoint:FormField.

I do have a question; does it matter if I use the single quote or double quotes?
0
 
LVL 9

Author Comment

by:macarrillo1
Comment Utility
Ah!  Turned out I misspelled the jquery src.  So now this works.
Now I need to make this an on change event and check if it is null.
0
 
LVL 51

Assisted Solution

by:tedbilly
tedbilly earned 500 total points
Comment Utility
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Author Comment

by:macarrillo1
Comment Utility
Ok,

How do I adapt this function to check if the field is Null?

$(document).ready(function()    {
     $("input[id*=ff5]").Change(function()    {
         $("div#ff5".hide();
         });
     $("input[id*=ff5]").Change(function()    {
         $("div#ff5".show();
         });

});
0
 
LVL 51

Accepted Solution

by:
tedbilly earned 500 total points
Comment Utility
There is no such thing as Null on the client.  You have to check for an empty string or a string length of 0
0
 
LVL 9

Assisted Solution

by:macarrillo1
macarrillo1 earned 0 total points
Comment Utility
I was able to get this to work with your suggestions and some additional informaiton from StackOverflow.

$(document).ready(function()    {
     $("input[id*=ff5]").Change(function()    {
          var f=$.trim(S("input[id*=ff5]").val()).length;
          if(f==0) {
              $("div#ff5").show();
          }
          else   {
              $("div#ff5").hide();
         }
         });
});

Thanks Again!

I will most likely be opening another question on Monday to adapt this function to validate multiple
fields instead of just a single specific field.
0
 
LVL 9

Author Closing Comment

by:macarrillo1
Comment Utility
I select my comment as part of the solution as it was a summation of all the suggestions needed to complete the solution.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now