Solved

verify text field is not empty before submit

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

726 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