-1

When the submit button is pressed, my validation function should check if the fields are validated then call the setProfile method. currently when i click the submit button it will not validate my fields so something must be wrong

       <form name="Login" method="post" action="#" onsubmit="return validateForm()">

        <input type="text" name="fName" id="name"> <br>
        </form>
        <input type="submit" name="Update" value="Update">

        function validateForm() {
        
        var n = document.forms['Login']['fName'].value;
        if(n==null || n=="")
        {
            alert("Please enter your name");
            return false;
        }
        return true
        } 
    function UpdateProfile() {
        document.querySelector('submit').addEventListener('click', e=>{
        const myProfile = new Profile 
        
             if (e.validateForm === true){
             myProfile.setProfile();}
             })
    }
  • Ok, what's the issue with the code you've provided here...? See [ask] – esqew Jun 10 '21 at 17:49
  • So you told us what it should do but nothing about what it actually is doing or how it differs from expected or any errors that occur – charlietfl Jun 10 '21 at 17:51
  • sorry, the issue is that the code as it is will not validate my field currently. i think i might have made the updateProfile function wrong, if i do an onsubmit on the form directly to my validateForm function it works, but not with the UpdateProfile function – Truls Pettersen Jun 10 '21 at 18:04
  • Can you update your post to include all your relevant code as a [mre]? You mention that you believe it's an issue with how your function is invoked, but there isn't any code here that shows *how* you've currently got that invocation piece set up. – esqew Jun 10 '21 at 18:12

1 Answers1

-1

The most likely reason for your code not working is that your validateForm() function is not getting called at all on submit button press. To find out if that's the case, the simplest thing to do is to add an alert() at the top of the validateForm() function.

If it's indeed not called, google "call javascript function on button click" for a code sample. Here's one: Using an HTML button to call a JavaScript function

Alex Weinstein
  • 9,823
  • 9
  • 42
  • 59