Solved

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

Posted on 2012-03-30
8
432 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
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)

734 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