1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Creating Form Box, so a user has to type before clicking "Submit"

Discussion in 'Programming' started by misohoni, Nov 12, 2005.

  1. #1
    Is there a way of making a text box which if a user clicks the Submit Button without typing any text, then it won't allow the search to take place.

    Can this be done without JS?
     
    misohoni, Nov 12, 2005 IP
  2. torunforever

    torunforever Peon

    Messages:
    414
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Given the current HTML/XHTML spec, you need a scripting language to enforce required fields. Maybe in the future it will be built in.

    The following javascript and xhtml is an example of preventing the submit if a text box is blank.

    
    function validateValue()
    {
    	return document.getElementById('idtextbox').value.length > 0;
    }
    
    Code (javascript):
    
    <form action="yourpage.php" method="post" onsubmit="return validateValue();">
    <input type="text" name="nametextbox" id="idtextbox" />
    <input type="submit" value="Submit" />
    </form>
    
    Code (markup):
     
    torunforever, Nov 12, 2005 IP
    misohoni likes this.
  3. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #3
    Ah I see, I thought it might be to do with onsubmit, but does it need to be controlled by JS?

    I thought it was built into forms, but maybe I was dreaming...perhaps I saw something in my version of Dreamweaver?
     
    misohoni, Nov 12, 2005 IP
  4. torunforever

    torunforever Peon

    Messages:
    414
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'm not aware of any built-in functionality that would do what you're asking. You're going to need to use javascript, or simply let the form be submitted, validate on the backend, then respond back with the same page but with warnings.

    If you're interested in reading up on a proposed specification called Web Forms 2.0 (submitted to the W3C by Mozilla and Opera), they have a required attribute listed.
    http://whatwg.org/specs/web-forms/current-work/#the-required

    I don't think any browsers support Web Forms 2.0 yet (maybe Opera 9?), so I wouldn't count on using the required attribute just yet.
     
    torunforever, Nov 12, 2005 IP
  5. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #5
    Yep, I'll probably use this code then:

    on the search form add:

    onsubmit="return checksearch();"

    The JS is:

    function checksearch() {
    e = document.getElementById('q');
    if(!/\S/.test(e.value)) {
    alert("You must enter a search term.");
    return(false);
    }
    return(true);
    }
     
    misohoni, Nov 12, 2005 IP
  6. flash_f

    flash_f Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    wow, this is realy cool
     
    flash_f, Dec 9, 2005 IP