Click here to Skip to main content
15,887,333 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to implement form validation for an online database of water wells. Part of the form relevant to my problem are two input type text elements. I attach onchange event handler and bind them to my script section at the bottom of the page just before the closing body tag.

input boxes:

HTML
<pre></div>
                                        <div class="form-group">
                                        <label for="hole_diam_mm" >Hole diameter (mm)</label>
                                            <input type="text" name="hole_diam_mm" id="hole_diam_mm" tabindex="4" class="form-control" placeholder="Hole diameter (mm)" 
                                            value=""  >
                                        </div>
                                        <div class="form-group">
                                        <label for="casing_diam_mm" >Casing diameter (mm)</label>
                                            <input onchange=casing_val() type="text" name="casing_diam_mm" id="casing_diam_mm" tabindex="4" class="form-control" placeholder="Casing diameter (mm)" 
                                            value=""  >
                                        </div>
                                        <div class="form-group">
                                        <label for="screen1_top_dbg" >Top of first screen</label>
                                            <input type="text" name="screen1_top_dbg" id="screen1_top_dbg" tabindex="5" class="form-control" placeholder="Top of first screen" 
                                            value = "" >
                                        </div>
                                        <div class="form-group">
                                        <label for="screen1_bot_dbg" >Bottom of first screen</label>
                                            <input onchange=firstscreen_val() type="text" name="screen1_bot_dbg" id="screen1_bot_dbg" tabindex="6" class="form-control" placeholder="Bottom of first screen" 
                                            value = "" >
                                        </div>


What I have tried:

Here is my javascript code. The first function works as expected, but there's no response to the second one.

HTML
<pre><script type="text/javascript">
            function casing_val() {
             //Set the element variables
            let xElement = document.getElementById("casing_diam_mm");
            let yElement = document.getElementById("hole_diam_mm");
            let x = parseFloat(xElement.value); // parseFloat converts string to floating number
            let y =  parseFloat(yElement.value);

            if (x > y) {
                xElement.value = ""; // use the element variable to set the value
               alert("casing diameter must be less than hole diameter. Try again.");
            }
            }
            
                        function firstscreen_val() {
                let st1Element = document.getElementById("screen1_top_dbg"); 
                let sb1Element = document.getElementById("screen1_bot_dbg");
                let zElement = document.getElementById("depth_dbg");
                let st1 = parsefloat(st1Element.value);
                let sb1 = parsefloat(sb1Element.value);
                let z = parsefloat(zElement.value);
            if (st1 > sb1) {
                sb1Element.value ="";
                alert("Bottom of screen must be lower than the top. Try again.");
                        }
                
            }     
            
                     
        </script>


It must be something simple, but I can't figure it out.
Posted
Comments
Dave Kreskowiak 12-Feb-24 22:52pm    
First things first. Hit F12 to being up the browser dev tools. Click on the Console tab and run your code. Are there any error messages in the console?
Nicholas Lombardi 12-Feb-24 23:39pm    
Hi Dave - there are two uncaught references both "parsefloat not defined" in the second function firstscreen_val and at the HTMLInputElement.onchange and HTMLInputElement.firstscreen_val.

No typos that I could see, the code for that and the first input text element is exactly the same, in the same position.
Dave Kreskowiak 12-Feb-24 23:49pm    
Names are case sensitive. "parseFloat" is correct, "parsefloat" is not.
Nicholas Lombardi 12-Feb-24 23:42pm    
First offending line is
let st1 = parsefloat(st1Element.value);
Nicholas Lombardi 12-Feb-24 23:48pm    
Okay - I see it parseFloat - not parsefloat - didn't capitalize the F

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900