Solved

verify text field is not empty before submit

Posted on 2008-09-30
3
1,482 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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 …
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…

920 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

13 Experts available now in Live!

Get 1:1 Help Now