Solved

jquery ui datepicker validation

Posted on 2011-09-14
3
439 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
  • 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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

831 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