• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 765
  • Last Modified:

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

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
RadhaKrishnaKiJaya
Asked:
RadhaKrishnaKiJaya
  • 6
  • 4
3 Solutions
 
Gurvinder Pal SinghCommented:
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
 
RadhaKrishnaKiJayaAuthor Commented:
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
 
leakim971PluritechnicianCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
leakim971PluritechnicianCommented:
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
 
leakim971PluritechnicianCommented:
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
 
RadhaKrishnaKiJayaAuthor Commented:
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
 
leakim971PluritechnicianCommented:
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
 
RadhaKrishnaKiJayaAuthor Commented:
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
 
leakim971PluritechnicianCommented:
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
 
RadhaKrishnaKiJayaAuthor Commented:
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
 
leakim971PluritechnicianCommented:
You're welcome! Thanks for the points!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now