www.webdeveloper.com
+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2012
    Posts
    7

    Form field default values - (can this be done?)

    Due to lack of space to use <labels> for my text fields, i've used the default value to label them (to give a description of what info is expected).
    I'm using javascript to clear the default text on focus.
    The problem i have, is i don't want the default values to be entered into the database.
    To add to the problem, i need to put validation on some of the text fields :-)

    a catch 22 maybe?

    Thanks in advance for any help

    Andy

  2. #2
    Join Date
    Jun 2012
    Posts
    7
    forgot to add the code:

    Code:
    <input name="CustEmail" type="text" id="CustEmail" value="enter customer email" size="46" onfocus="if(this.value==this.defaultValue)this.value=''" onblur="if(this.value=='')this.value=this.defaultValue"/>

  3. #3
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,078
    Use something like this
    Code:
    <body>
    <form action="#" onsubmit="return !!document.getElementById('errMsg').innerHTML">
    <p><input  name="CustEmail" type="text" id="CustName" value="Customer name" size="32" onfocus='rmvDfl(this)' onblur="excTst(this)">
    <p><input  name="CustEmail" type="text" id="CustEmail" value="Customer email" size="32" onfocus='rmvDfl(this)' onblur="excTst(this)">
    <p id="errMsg"></p>
    <p><input type="submit" value="Submit"></p>
    </form>
    <script type="text/javascript">
    function rmvDfl(t){// remove default value and restore the cursor in the field
    	if (t.value==t.defaultValue) {t.value="";
    		if (document.selection && document.selection.clear) document.selection.clear();}
    }
    function excTst(t){// restore default values and execute tests for validation
       if (t.value=='') t.value=t.defaultValue;
    	else {// Custom test
    		if (t.value==t.defaultValue) {document.getElementById('errMsg').innerHTML='Fills all the fields !';return;}
     		// Other tests
    		if (t.id=='CustName') {// validation for Name
    		}
    		else if (t.id=='CustEmail') {// validation for Email
    			if (/*not valid*/true) document.getElementById('errMsg').innerHTML='The Email seems erroneus !';
    		}
    	}
    }
    </script>
    </body>
    Which is, If I am not mistaken, cross browsers and allow to use the tab key to walk thought the form...
    Last edited by 007Julien; 07-27-2012 at 10:53 AM. Reason: Complements

  4. #4
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,062
    Quote Originally Posted by Andyjay View Post
    The problem i have, is i don't want the default values to be entered into the database.
    Your server-side validation should be preventing that.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,078
    I agree, the real validation has to take place on the server. It is nevertheless useful, for client and server, to work on the client side to avoid useless calls...

  6. #6
    Join Date
    Jun 2012
    Posts
    7
    Thanks 007Julien

    Much appreciated :-)
    Andy

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



Recent Articles