Solved

Validate Date, US Phone# and Radio Button in jQuery PlugIn

Posted on 2010-09-24
13
719 Views
Last Modified: 2012-05-10
Hi Experts,
I need the followings to validate using jQuery PlugIn.  Please try to help me.

1. Date in MM/DD/YYYY format.
2. Us Phone Number 999-999-9999 format.
3. Radio Button (if not selected)

Any kind of help is appreciated.  Thank you in advance.
0
Comment
Question by:RadhaKrishnaKiJaya
  • 6
  • 4
13 Comments
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
1) check this
http://stackoverflow.com/questions/511439/custom-date-format-with-jquery-validation-plugin

the reg-ex format would be
\d{2}/\d{2}\\d{4}

2) similarly the reg-ex for US phone number will be
\d{3}\-\d{3}\-\d{4}
check this also
http://docs.jquery.com/Plugins/Validation/CustomMethods/phoneUS

3) http://stackoverflow.com/questions/277589/validation-of-radio-button-group-using-jquery-validation-plugin
this will be a straightforward one

you need to add
radioButtonId : true,

in the rules
0
 

Author Comment

by:RadhaKrishnaKiJaya
Comment Utility
Hi gurvinder372,
I tested all of the above.  
1.  The date validation did not work at all.  In fact the example code contains date validation for an Australian Date format.  Also, this is not an working code.  Do you have any other suggestion?
2.  The phone# working fine.  Thank You.
3.  The RadioButton selection works when Html tag is used.  It doesn't work when ASP tags are used such as see the codes below.  Please provide me any other solution if you have.  

Thank you gurvinder372 for your help.

                                          <tr>
                                                <td align="center" colSpan="2"><asp:panel id="Panel1" runat="server"><FONT class="label">
                                                                  <asp:RadioButton id="rbUnitedStates" runat="server" TextAlign="Left" Text="United States" checked
                                                                        GroupName="Region"></asp:RadioButton>&nbsp;&nbsp;
                                                                  <asp:RadioButton id="rbCanada" runat="server" TextAlign="Left" Text="Canada" GroupName="Region"></asp:RadioButton>&nbsp;&nbsp;
                                                            </FONT>
                                                      </asp:panel></td>
                                          </tr>
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
Check this plugin : http://digitalbush.com/projects/masked-input-plugin/

>3.  The RadioButton selection works when Html tag is used.  It doesn't work when ASP tags are used such as see the codes below.  Please provide me any other solution if you have.

Put you radio button in a div with the id of the groupname (but you can put another id...) :

Here the div with id << Region >> :


<tr>
<td align="center" colspan="2">
    <asp:panel id="Panel1" runat="server">
            <div id="Region">
                <font class="label">
                <asp:RadioButton id="rbUnitedStates" runat="server" TextAlign="Left" Text="United States" GroupName="Region"></asp:RadioButton>&nbsp;&nbsp;
                <asp:RadioButton id="rbCanada" runat="server" TextAlign="Left" Text="Canada" GroupName="Region"></asp:RadioButton>&nbsp;&nbsp;
                </font>
            </div>
    </asp:panel>
</td>
</tr>

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
and use :


<script language="javascript" type="text/javascript">
    function validateRBChecked(groupname) {
        var inputs = document.getElementById(groupname).getElementsByTagName("input");
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].type == "radio" && inputs[i].checked) return true;
        }
        alert("Choose a " + groupname);
        return false;
    }
</script>

Open in new window

0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 500 total points
Comment Utility
test page :


<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script language="javascript" type="text/javascript">
    function validateRBChecked(groupname) {
        var inputs = document.getElementById(groupname).getElementsByTagName("input");
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].type == "radio" && inputs[i].checked) return true;
        }
        alert("Choose a " + groupname);
        return false;
    }
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<table>
<tr>
<td align="center" colspan="2">
    <asp:panel id="Panel1" runat="server">
            <div id="Region">
                <font class="label">
                <asp:RadioButton id="rbUnitedStates" runat="server" TextAlign="Left" Text="United States" GroupName="Region"></asp:RadioButton>&nbsp;&nbsp;
                <asp:RadioButton id="rbCanada" runat="server" TextAlign="Left" Text="Canada" GroupName="Region"></asp:RadioButton>&nbsp;&nbsp;
                </font>
            </div>
    </asp:panel>
</td>
</tr>
</table>
<asp:Button ID="mySubmit" runat="server" Text="Submit" OnClientClick="return validateRBChecked('Region')"  />
</asp:Content>

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:RadhaKrishnaKiJaya
Comment Utility
Hi leakim971, Thank you for your reply.  I tried your above code.  In my case it did not compile.  So, I modified the code little bit as below.  Still the Javascript code is not executed on Submit button click.  Please try to fix this code if possible.  Thank you very much for your help.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
      <HEAD>
            <title>demo16</title>
            <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
            <meta name="CODE_LANGUAGE" Content="C#">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">

            <script language="javascript" type="text/javascript">
                  function validateRBChecked(groupname) {
                        alert("Inside Javascript code...");
                        var inputs = document.getElementById(groupname).getElementsByTagName("input");
                        for (var i = 0; i < inputs.length; i++) {
                        if (inputs[i].type == "radio" && inputs[i].checked) return true;
                      }
                      alert("Choose a " + groupname);
                        return false;
                  }
            </script>
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <td align="center" colspan="2">
                        <asp:panel id="Panel1" runat="server">
                              <DIV id="Region">
                                    <asp:RadioButton id="rbAmerica" style="Z-INDEX: 101; LEFT: 72px; POSITION: absolute; TOP: 48px" runat="server"
                                          GroupName="Region"></asp:RadioButton></DIV>
                        </asp:panel>
                  </td>
                  <asp:RadioButton id="rbCanada" style="Z-INDEX: 102; LEFT: 184px; POSITION: absolute; TOP: 48px" runat="server"
                        GroupName="Region"></asp:RadioButton>
                  <asp:Button ID="mySubmit" runat="server" Text="Submit" OnClientClick="return validateRBChecked('Region')"  />
            </form>
      </body>
</HTML>
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
Very strange your page run fine for me...

below the generated page, you may compare it with your...
I'm going to sleep, see you later.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 

<HTML> 

      <HEAD> 

            <title>demo16</title> 

            <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> 

            <meta name="CODE_LANGUAGE" Content="C#"> 

            <meta name="vs_defaultClientScript" content="JavaScript"> 

            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> 

 

            <script language="javascript" type="text/javascript"> 

                function validateRBChecked(groupname) {

                    alert("Inside Javascript code...");

                    var inputs = document.getElementById(groupname).getElementsByTagName("input");

                    for (var i = 0; i < inputs.length; i++) {

                        if (inputs[i].type == "radio" && inputs[i].checked) return true;

                    }

                    alert("Choose a " + groupname);

                    return false;

                }

            </script> 

      </HEAD> 

      <body > 

            <form method="post" action="Default4.aspx" id="Form1"> 

<div class="aspNetHidden"> 

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE3NjU1MjYzMDBkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYEBQlyYkFtZXJpY2EFCXJiQW1lcmljYQUIcmJDYW5hZGEFCHJiQ2FuYWRhU8+xJZowthrzyJyx6jmGW783FORmeBKafphkuMnHrZc=" /> 

</div> 

 

<div class="aspNetHidden"> 

 

	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBAK5hq/kAwLH9YqIAwLfi+nVCAKRq/PlBzY+e88TgbZiEvdHZfvpl0bs2vpkXxaEt6xb6825VS82" /> 

</div> 

                  <td align="center" colspan="2"> 

                        <div id="Panel1"> 

	

                              <div id="Region"> 

                                    <span style="Z-INDEX: 101; LEFT: 72px; POSITION: absolute; TOP: 48px"><input id="rbAmerica" type="radio" name="Region" value="rbAmerica" /></span></DIV> 

                        

</div> 

                  </td> 

                  <span style="Z-INDEX: 102; LEFT: 184px; POSITION: absolute; TOP: 48px"><input id="rbCanada" type="radio" name="Region" value="rbCanada" /></span> 

                  <input type="submit" name="mySubmit" value="Submit" onclick="return validateRBChecked(&#39;Region&#39;);" id="mySubmit" /> 

            </form> 

      </body> 

</HTML>

Open in new window

Clipboard08.jpg
0
 

Assisted Solution

by:RadhaKrishnaKiJaya
RadhaKrishnaKiJaya earned 0 total points
Comment Utility
Hi leakim971,
Thank you for your help.  The code did not execute on my machince for some reason.  Now, I changed the code to execute the on submit rather than on click.  It is working now.  But, it is all your code only.  The 100% credit goes to you.  See the working code below.  Probably you can take a look the code at your leisure time.  Thank you very much for your help.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
      <HEAD>
            <title>demo16</title>
            <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
            <meta name="CODE_LANGUAGE" Content="C#">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
            <script language="C#" runat="server">
   
            void Page_Load(object s, EventArgs e)
            {
            }

            private void btnClear_Click(object sender, System.EventArgs e)
            {
                }

            private void btnSend_Click(object sender, System.EventArgs e)
            {
                            string emailBody;
                           string toAddress;
                           string fromAddress;
                           string subject;

                           fromAddress = "new.user@bcdtravel.com";

                           subject = "New User Request from ";
                     toAddress = "online.travel@turner.com";
                     toAddress = "satyabrata.mahapatra@bcdtravel.com ";
                }
            </script>
            <script language="javascript" type="text/javascript">
                  function validateRBChecked(groupname) {
                        //alert("Inside Javascript code...");

                        var inputs = document.getElementById(groupname).getElementsByTagName("input");
                      //alert("inputs.length: " + inputs.length);

                        for (var i = 0; i < inputs.length; i++) {
                        if (inputs[i].type == "radio" && inputs[i].checked) return true;
                      }
                      alert("Choose a " + groupname);
                        return false;
                  }
            </script>
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server" OnSubmit="return validateRBChecked('Region')">
                  <td align="center" colspan="2">
                        <asp:panel id="Panel1" runat="server">
                              <DIV id="Region">
                                    <asp:RadioButton id="rbAmerica" style="Z-INDEX: 101; LEFT: 72px; POSITION: absolute; TOP: 48px" runat="server"
                                          GroupName="Region"></asp:RadioButton>
                                    <asp:RadioButton id="rbCanada" style="Z-INDEX: 102; LEFT: 184px; POSITION: absolute; TOP: 48px" runat="server"
                                          GroupName="Region"></asp:RadioButton></DIV>
                        </asp:panel>
                  </td>
                  <asp:button id="btnSend" onclick="btnSend_Click" runat="server" Text="Submit" tabIndex="36"
                        style="Z-INDEX: 103; LEFT: 144px; POSITION: absolute; TOP: 88px"></asp:button>
            </form>
      </body>
</HTML>
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
The submit buttons :
<asp:Button ID="mySubmit" runat="server" Text="Submit" OnClientClick="return validateRBChecked('Region')"  />

versus

<asp:button id="btnSend" onclick="btnSend_Click" runat="server" Text="Submit" tabIndex="36"
                        style="Z-INDEX: 103; LEFT: 144px; POSITION: absolute; TOP: 88px"></asp:button>

Open in new window

0
 

Author Closing Comment

by:RadhaKrishnaKiJaya
Comment Utility
Thoug I am saveral questions in this case, and some are yet to be answered, I am going to close this case and open another for the rest of my questions to avoid confusion.  Thank you all for your wonderful support.
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
You're welcome! Thanks for the points!
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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article discusses how to create an extensible mechanism for linked drop downs.
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)
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…

771 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

11 Experts available now in Live!

Get 1:1 Help Now