Web Analytics Made Easy -
StatCounter Complete beginner! - CodingForum

Announcement

Collapse
No announcement yet.

Complete beginner!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Complete beginner!

    Hi there,

    I am a complete beginner to Javascript so I'm trying to figure out form validation for a simple form I have.

    I have two functions below that I want to be called when I submit the form but how can I make them into one function?

    Here's the code:

    <script type="text/javascript">
    <!--
    function validate(){
    if ((document.Sapone.FirstName.value=="")||
    (document.Sapone.LastName.value=="")){
    alert ("You must fill in all of the required fields!")
    return false
    }
    else
    return true
    }

    function emailcheck(){
    var string1=document.Sapone.email.value
    if (string1.indexOf("@")==-1){
    alert("Please input a valid email address!")
    document.Sapone.email.onsubmit()
    }
    }
    </script>

    Thanks a mill for any help!

  • #2
    Very simply - just combine them.

    But validation such as if ((document.Sapone.FirstName.value=="") is virtually useless as even a single space or a ? or something silly like $%&* will return false (i.e. pass the validation). Likewise an email address must contain far more than an @ to be valid.

    Code:
    <script type="text/javascript">
    
    function validate(){
    var fn = document.Sapone.FirstName.value;
    var ln = document.Sapone.LastName.value;
    fn = fn.replace(/^\s+|\s+$/g,"");  // strip leading and trailing spaces
    ln = ln.replace(/^\s+|\s+$/g,"");  // strip leading and trailing spaces
    fn = fn.replace(/[^a-z\-]/gi,"");  // strip anything but a-z and hyphen  Mary-Lou
    ln = ln.replace(/[^a-z\-\']/gi,""); // strip anything but a-z hyphen apostrophe O'Reilly Smith-Jones
    document.Sapone.FirstName.value = fn;  // write back adjusted text into the field
    document.Sapone.LastName.value = ln;
    if ((fn.length <3) || (ln.length < 3)) {   // minimum 2 acceptable characters
    alert ("You must fill in all of the required fields!");
    return false;  // i.e. stop here if validation of name fields fails
    }
    
    var string1=document.Sapone.email.value;
    if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(string1)) { 
    alert("Please input a valid email address!")
    document.Sapone.email.focus();
    return false;
    }
    return true  // the default so not required.
    }
    </script>
    Your onsubmit should read onsubmit="return validate()";

    Please read the posting guidelines, especially those relating to silly thread titles and wrapping your code in [code] tags. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose.


    "I'm such a good lover because I practice a lot on my own." - Woody Allen
    Last edited by Philip M; Apr 4, 2009, 02:56 AM. Reason: Add missing comment tags

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

    Comment


    • #3
      Javascript for Form Validation - beginner

      Hi there,

      I've tried this but it's not validating at all - it just let's me submit the form as usual.

      Am I doing something wrong?

      Comment


      • #4
        Originally posted by Fun Leprechaun View Post
        Hi there,

        I've tried this but it's not validating at all - it just let's me submit the form as usual.

        Am I doing something wrong?
        Yes, very probably.

        But I see that I have left out some comment tags // which you need to add:-

        fn = fn.replace(/[^a-z\-]/gi,""); // strip anything but a-z and hyphen Mary-Lou
        ln = ln.replace(/[^a-z\-\']/gi,""); // strip anything but a-z hyphen apostrophe O'Reilly Smith-Jones

        That will stop it working! When these are corrected it works fine.
        But you would have found that yourself if you had JavaScript error reporting turned on.

        To test, put the line

        alert ("Validate function started");
        after
        function validate(){

        Does the alert show? If not you are not calling the function correctly.
        Last edited by Philip M; Apr 4, 2009, 03:10 AM.

        All the code given in this post has been tested and is intended to address the question asked.
        Unless stated otherwise it is not just a demonstration.

        Comment

        Working...
        X