?
Solved

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

Posted on 2012-03-30
8
Medium Priority
?
434 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
LVL 51

Assisted Solution

by:Ted Bouskill
Ted Bouskill earned 2000 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article discusses how to create an extensible mechanism for linked drop downs.
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)
Suggested Courses

764 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