Solved

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

Posted on 2012-03-30
8
430 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:Ted Bouskill
ID: 37791294
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
ID: 37791343
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
ID: 37791377
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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 51

Assisted Solution

by:Ted Bouskill
Ted Bouskill earned 500 total points
ID: 37791387
0
 
LVL 9

Author Comment

by:macarrillo1
ID: 37791431
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:
Ted Bouskill earned 500 total points
ID: 37791614
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
ID: 37791678
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
ID: 37810110
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

809 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