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 7 of 7
  1. #1
    Regular Coder
    Join Date
    May 2009
    Posts
    813
    Thanks
    123
    Thanked 24 Times in 24 Posts

    Simple validation query

    I have a simple validation script. The first line of the validation is:

    function Validate()
    {
    if (document.form.name.value == '')
    {

    the id of my form is form, so my query is will this validation work, as it does not have a name but it has an id. Should I change the name (above) to id?

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,773
    Thanks
    19
    Thanked 155 Times in 146 Posts
    Without a "name" for your form, that code won't work. I would use a name for your form that is identical to the form's id.

    Also, in general, you should avoid using an "id" or "name" that also refers to an element or attribute in the DOM.

    So, putting the two ideas together, try something like this:

    In your HTML:

    Code:
    <form id="form1" name="form1" method="post" action="file.php">
       <p>Username: <input type="text" name="username" /></p>
    </form>
    In your JS:

    Code:
    if (document.form1.username.value == '')
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • Users who have thanked chump2877 for this post:

    quartzy (03-06-2011)

  • #3
    Regular Coder
    Join Date
    May 2009
    Posts
    813
    Thanks
    123
    Thanked 24 Times in 24 Posts
    Thanks for your post, I did wonder about that small thing.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Form validation of the pattern if (document.form1.username.value == '') is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

  • #5
    Regular Coder
    Join Date
    May 2009
    Posts
    813
    Thanks
    123
    Thanked 24 Times in 24 Posts
    Sorry I do not fully understand you, are you saying that my code is not going to work? I am not a regular member of this forum, so would not know if the validation has been covered many times either. I could delve into regular expressions but it is just for a simple form, and so would work OK I feel. with fields of name email and message.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Read what I said. You code will work, but is virtually useless as if the user enters even a single space or a ? sign it will pass the validation.

    You can use the search feature of this forum to find out more about how to validate form fields.

    Quote Originally Posted by quartzy View Post
    I am not a regular member of this forum
    639 posts and not a regular user?
    Last edited by Philip M; 03-06-2011 at 05:07 PM.

  • #7
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Hi quartzy,

    I usually do something along these lines for my client side validation. Instead of alerting the errors, another option is to highlight the input labels in the form with a different colour, typically red.

    Code:
     
                function validateForm(){
                    var inp1 = document.getElementById('txtInp1').value;
                    //... similarly for other inputs to be validated
                    var errMsg = "The following inputs are invalid:\n\n"
                    var errors = new Array();
                    var isDataValid = true;
                    //validate inp1
                    if(/[^0-9]/.test(inp1)){
                        isDataValid = false;
                        errors.push('inp1');
                    }
                    //validate the other inputs
                    //...
                    //display any error messages
                    if(!isDataValid){
                        for(i=0; i < errors.length; i++){
                            errMsg += errors[i] + "\n";
                        }
                        alert(errMsg);
                    }
                    return isDataValid;
                }
    Last edited by bullant; 03-06-2011 at 07:34 PM.


  •  

    Posting Permissions

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