Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Sep 2002
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Validate Textbox

    This is the JavaScript code which ensures that users don't keep a textbox vacant while submitting a Form (this is the onSubmit event function of the Form):
    Code:
    if(document.forms[0].txt1.value==""){
        alert("Textbox cannot be blank!");
        document.forms[0].txt1.focus();
        return false;
    }
    As such the above code works fine & prompts the user with the alert message if he tries to submit the Form without entering any text in the textbox but if the textbox is filled with spaces (by pressing the Spacebar key on the keyword), then the validation fails.

    How do I overcome this?

    Thanks,

    Arpan

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    You could check for numbers or text:
    Code:
    var input = document.forms[0].txt1.value;
    if(input.search(/\d|\w/) == -1) {
        alert("Textbox cannot be blank!");
        document.forms[0].txt1.focus();
        return false;
    }
    this will allow spaces

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Or you can attach a trim function to the String object.

    Code:
    String.prototype.trim = function() {
    	return this.replace(/^\s+|\s+$/g,"");
    }
    ...
    if(document.forms[0].txt1.value.trim()==""){
        alert("Textbox cannot be blank!");
        document.forms[0].txt1.focus();
        return false;
    }

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by rwedge View Post
    You could check for numbers or text:
    Code:
    var input = document.forms[0].txt1.value;
    if(input.search(/\d|\w/) == -1) {
        alert("Textbox cannot be blank!");
        document.forms[0].txt1.focus();
        return false;
    }
    this will allow spaces
    \w is equivalent to a-zA-Z0-9 and underscore. So \d is redundant. But a single valid character (even a _ ) in the textbox will return true.

    Suggested improvement:-

    if ((input.search(/\w/) == -1) || (input.length < 6)) {

    Replace 6 by a number which is sensible in the context.
    Last edited by Philip M; 05-06-2007 at 09:23 AM.

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    So \d is redundant.
    Thanks for pointing that out Philip. Alphanumeric+_, I latched onto words and was not thinking.

    The requirements for an empty field is a little vague, what's expected.etc..
    The length bit would be true if there were one character and the rest spaces.
    For all we know, this field could be a students grade and A+ would be acceptable.

    I think the example glenngv posted is a good approach. Reuseable, it could get rid of unwanted spaces and will catch an empty field.
    Last edited by rwedge; 05-06-2007 at 08:38 PM.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by rwedge View Post
    I think the example glenngv posted is a good approach. Reuseable, it could get rid of unwanted spaces and will catch an empty field.
    Yes, but a single character, even a ? or :<) will return true. My version disregards spaces and will catch a field which is empty or of 'inadequate' length. You could of course strip any spaces as well.

    I still prefer to set a minimum (or possibly maximum) length for the input depending on what is sensible or expected under the circumstances. A student's grades should accept only letters A to E as the first character, then only + and - three characters max (not min).

    if (/^([A-E][\-\+]?[\-\+]?)$/i.test (grades.value)) {

    An address might reasonably require 6 characters minimum. A personal name may not contain numerals (I think!). In short, the precise form of validation ought to be sensitive to the expected answer.
    Last edited by Philip M; 05-07-2007 at 08:17 AM. Reason: Improve clarity


  •  

    Posting Permissions

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