?
Solved

verify text field is not empty before submit

Posted on 2008-09-30
3
Medium Priority
?
1,497 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
[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
3 Comments
 
LVL 4

Accepted Solution

by:
sijishJohn earned 200 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…
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…
Suggested Courses

771 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