HTML/[removed] Simple form validation on submit

后端 未结 6 2155
北荒
北荒 2020-11-29 05:40

I\'m trying to validate my form with the easiest way possible, but somehow it is not working and when I click submit it just takes me to the next page without giving the ale

6条回答
  •  借酒劲吻你
    2020-11-29 06:09

    You have several errors there.

    First, you have to return a value from the function in the HTML markup:

    Second, in the JSFiddle, you place the code inside onLoad which and then the form won't recognize it - and last you have to return true from the function if all validation is a success - I fixed some issues in the update:

    https://jsfiddle.net/mj68cq0b/

    function validateURL(url) {
        var reurl = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
        return reurl.test(url);
    }
    
    function validateForm()
    {
        // Validate URL
        var url = $("#frurl").val();
        if (validateURL(url)) { } else {
            alert("Please enter a valid URL, remember including http://");
            return false;
        }
    
        // Validate Title
        var title = $("#frtitle").val();
        if (title=="" || title==null) {
            alert("Please enter only alphanumeric values for your advertisement title");
            return false;
        }
    
        // Validate Email
        var email = $("#fremail").val();
        if ((/(.+)@(.+){2,}\.(.+){2,}/.test(email)) || email=="" || email==null) { } else {
            alert("Please enter a valid email");
            return false;
        }
      return true;
    }
    

提交回复
热议问题