Solved

jquery ui datepicker validation

Posted on 2011-09-14
3
442 Views
Last Modified: 2012-05-12
Hi Experts,

I'm using a standard Jquery-UI datepicker and I'd like to add validation to the text field because visitors can just type any number in the field.

How can I do this?

Thanks,

HNM
0
Comment
Question by:HelpNearMe
[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
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36536385
set the onsubmit attribute of your form :

<form onsubmit="return validate()">

and use :

<script>
function validate() {
     var dateField = document.getElementsByTagName("myDate")[0].value;
     var dateFieldisValid =  /^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/.test(dateField); // YYYY-MM-DD
     if( ! dateFieldisValid ) alert( "please enter a valide date" );
     return dateFieldisValid; 
}
</script>

Open in new window

0
 

Author Comment

by:HelpNearMe
ID: 36537330
leakim971:

Thanks but that doesn't seem to work.  I've attached the form code perhaps you can see where the problem is?

BTW... it's not a jquery data picker like I thought it was.  And I didn't code this page.. I know this page.. it's horrible.  I've been asked to add date validation to it.

Thanks,

HNM
<!-- testing datepicker validation -->
<script type="text/javascript">
    function validate() {
        var dateField = document.getElementsByTagName("wedding_date")[0].value;
        var dateFieldisValid = /^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/.test(dateField); // YYYY-MM-DD
        if (!dateFieldisValid) alert("please enter a valid date");
        return dateFieldisValid;
    }
</script>

<!-- end -->


<form action="recibe_form.php" method="post" name="Registro" id="Registro" onsubmit="return validate()">

<table border="0" cellpadding="1" cellspacing="1">

                          <tbody>

                            <tr>

                              <td valign="middle" width="200"><strong> Wedding Date </strong> </td>

                              <td valign="middle"><input name="wedding_date" type="text" id="wedding_date" class="Texto_contenido requerido"  value="" size="10" />

                                    <img src="https://www.logiforms.com/jsimages/calendaricon.gif" alt="3" name="wedding_date_calimage" hspace="4" border="0" id="wedding_date_calimage" /></td>
                            </tr>
                          </tbody>
                        </table>

<input type="submit" class="Texto_titulo_index14Copy" value="Submit" name="Submit" />

</form>

Open in new window

0
 

Author Comment

by:HelpNearMe
ID: 36537341
The script is in the <head> section.. and of course all of this is wrapped in relevant body tags etc... the page works, except for the validation.

Thanks!

HNM
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

740 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