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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Validation form to only allow alpha characters and space and '. I am new to this.

    Thank you for your reply.

    With the code I had to acknowledge nothing had been inserted,(that is, still spaces, it was working. When I added the code to only accept alpha chars etc., I inserted numbers, nothing worked, with no error message.

    Would you, or can I send the whole code through for the form. I noticed that the form tags had been changed so I altered them as well according to your message.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    As I said in your previous post, "Well, it works for me."

    Post the relevant code (not the whole form please).

  • #3
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    allow only alpha chars in Last name text box. (javascript form validation)

    Thank you very much. The form was working and it came up with an error message if there were only spaces in Lastname. Then I put the code in to validate for chars only in Lastname.

    Your help is very much appreciated as I am very new to this.


    <form name="myForm" action="" method="post" onsumbit="return validateForm()" enctype="multipart/form-data" id="myForm">

    var x = document.forms["myForm"]["lastname"].value;
    x = x.replace(/^\s+|\s+$/g,""); // strip all leading and trailing spaces
    x = x.replace(/\s{2,}/g," "); // replace multiple spaces with one space
    if (x.length < 2) {
    alert("Last name must be filled out");
    setTimeout('document.forms["myForm"]["lastname"].focus()', 25); // refocus on it
    return false;
    }
    if (/[^a-z\s\-\']/gi.test (x)) { // only letters space hyphen apostophe allowed - Smith-Jones O'Flanaghan
    alert ("You may only enter letters, space, hyphen, apostrophe");
    document.forms["myForm"]["lastname"].value = ""; // clear the field
    setTimeout('document.forms["myForm"]["lastname"].focus()', 25); // and refocus on it
    return false;
    }



    <td colspan="2" align=center>
    <INPUT value="Submit" type="submit" onClick="return validateForm()">
    <INPUT onclick="formreset()" value="Reset" type="reset"></TD>

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    That's not close to legal code.

    You don't have <script>...</script> tags around the JavaScript code.

    You don't have a function named validateForm. Oh, you have the body of it, but not the head of it with the name.

    You don't need to call the function from both the submit button *and* the form's onsubmit. One or the other. Form's onsubmit preferred.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function validateForm(form)
    {
        var x = form.lastname.value;
        x = x.replace(/^\s+|\s+$/g,""); // strip all leading and trailing spaces
        x = x.replace(/\s{2,}/g," "); // replace multiple spaces with one space
        if (x.length < 2) {
           alert("Last name must be filled out");
           return false;
        }
        if (/[^a-z\s\-\']/gi.test (x)) { // only letters space hyphen apostophe allowed - Smith-Jones O'Flanaghan
             alert ("You may only enter letters, space, hyphen, apostrophe in the last name field");
             return false;
        }
        return true; 
    }
    </script>
    </head>
    <body>
    <form name="myForm" action="" method="post" onsumbit="return validateForm(this)" enctype="multipart/form-data">
    ...
    <input name="lastname">
    <INPUT value="Submit" type="submit">
    <INPUT onclick="formreset()" value="Reset" type="reset">
    ...
    </form>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Old Pedant - Why have you removed the "clear the field and refocus on it" lines?

    Quote Originally Posted by hmpk View Post
    Thank you very much. The form was working and it came up with an error message if there were only spaces in Lastname. Then I put the code in to validate for chars only in Lastname.
    It is not possible that the code worked at all as you have shown it.
    Last edited by Philip M; 04-22-2011 at 07:24 AM.

  • #7
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for your reply.
    The full source code is long and I didn't post all of it.

    I made the changes you suggested and it is still not working for me.

    I don't enter anything and it doesn't even pick up that there has been nothing selected in the first drop down box.

    Is there somewhere I can post the full source code to you?

    Thanks

    Hmpk

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Old Pedants's code contains a typo

    <form name="myForm" action="" method="post" onsumbit="return validateForm(this)"

    That is why I myself always test my code in a browser before posting it.
    I find that I often type docuemnt instead of document.


  •  

    Posting Permissions

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