Link to home
Start Free TrialLog in
Avatar of Enflow
EnflowFlag for United States of America

asked on

ASP.net How pass clientID to jScript in textbox onblur event

ASP.net textbox onblur JS function... Need to grab textbox ID and send it to function rather than hardcode the ID in the param pass for each input object

So have this and it works with hard code name of textbox id...

onblur="CheckEnteredDate(this,'CBirthDate_1');"

would i use <%  %> ??

for this function..

function CheckEnteredDate(passed, myID) {
        //alert("This is my input value = " + myID);
        if (new Date(passed.value) > new Date()) {
            alert('STOP - The Birth Date You Entered Is Greater Than Today!');
            document.getElementById(myID).value = "";
            return false;
        }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
another way :
<!DOCTYPE html>
<html>
<head>
    <script>
        window.addEventListener("DOMContentLoaded", function() { // https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded
            function CheckEnteredDate() {
                if (new Date(this.value) > new Date()) {
                    alert('STOP - The Birth Date You Entered Is Greater Than Today!');
                    this.value = "";
                    return false;
                }
            }
            var dates = document.querySelectorAll("input[name^='CBirthDate_']");
            for(var i=0;i<dates.length;i++)
                dates[i].addEventListener("blur", CheckEnteredDate); //  https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
        });
    </script>
</head>
<body>
<form>
    <input type="date" name="CBirthDate_1" />
    <input type="date" name="CBirthDate_2" />
    <input type="date" name="CBirthDate_3" />
    <input type="date" name="CBirthDate_4" />
</form>
</body>
</html>

Open in new window

Add  ClientIDMode="Static" to asp:Textbox control.
Avatar of Enflow

ASKER

@JCGD...

Why Add and Why Static... JS is working... ?
asp:TextBox has the ClientIDMode attribute to modify the way the ID is named. The "Static" value indicates that ASP do not change the value of the ID. Therefore, in the CheckEnteredDate you could have used getElementById("CBirthdate_1").value.

But the solution I think so is better