Solved

verify text field is not empty before submit

Posted on 2008-09-30
3
1,479 Views
Last Modified: 2010-04-21
Hi,

I have a text field
<input type="text" name="date" id="data" size="12" readonly onclick="popupcalendar('mydata')" />

which when clicked pops up a calendar. Below that is a submit button..

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

If I click submit and the user has selected a date then no problem...if I click submit with no date is selected then it throws an error.

How can I verify that the user has selected a date (or the textbox is not empty) before going ahead with the submit....

All I'm really looking for is a simple alert box saying "Please select a date"


Thanks,

0
Comment
Question by:thegunner
3 Comments
 
LVL 4

Accepted Solution

by:
sijishJohn earned 50 total points
ID: 22604199
Add this script to your page

<script language="javascript" type="text/javascript">
    function ValidateDate()
    {
        if(document.getElementById('data').value == "")
        {
            alert('Please select a date');
            return false;
        }
    }
   
    </script>


and call it in onclick event of the button...

<input type="submit" name="submit" value="" class="submit" onclick="return ValidateDate()"/>
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 22604572
You can also submit after validating the input,


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

    function ValidateDate()

    {

        if(document.formname.data.value == "")

        {

            alert('Please select a date');

            return false;

        }

		

		document.frmname.submit();

		return true;

    }

   

    </script>
 
 

<input type="button" name="submit" value="Save" class="submit" onclick="return ValidateDate()"/>

Open in new window

0
 

Author Closing Comment

by:thegunner
ID: 31501504
cheers...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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…

708 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

12 Experts available now in Live!

Get 1:1 Help Now