Below is the code how I am creating the dynamic textboxes. I want to validate if any of the dynamic textboxes created are empty and if yes, should throw an error message or alert to user prompting to fill up the textboxes on Additem button click. I get the alert message entered into the function, after which I don't receive any message alerting the user to fill in the textbox "xxxx".
Please help me to fix the code
if (ControlType == "TextBox")
{
int textBoxLength;
TextBox MynewTextBox = new TextBox();
MynewTextBox.ID = "txt" + Fldname;
MynewTextBox.Width = 100;
MynewTextBox.BorderStyle = BorderStyle.Solid;
MynewTextBox.Attributes.Add("Type", "T");
MynewTextBox.Attributes.Add("IsKeyField", "Y");
MynewTextBox.Attributes.Add("IsMandatory", "Y");
}
protected void Page_Load(object sender, EventArgs e)
{
btnAddItem.Attributes.Add("onClick", "if(!ValidateMandatoryFields()) return false;");
}
<script language="javascript" type="text/javascript">
function ValidateMandatoryFields() {
alert("entered into the function");
var inputControls = document.getElementsByTagName("input");
alert(inputControls.length);
for (var i = 0; i < inputControls.length; i++) {
if (inputControls[i].getAttribute("IsKeyField") == "Y") {
if (inputControls[i].getAttribute("Type") == "T" || (inputControls[i].getAttribute("Type") == "C")) {
if (inputControls[i].getAttribute("IsMandatory") == "Y") {
if (inputControls[i].value == "") {
alert(inputControls[i].getAttribute("KeyField_Name") + "is required.");
errorMsg += "\n" + inputControls[i].getAttribute("KeyField_Name") + " is required.";
isValidated = false;
}
}
}
}
}
}
}
</script>
enter link description here
i followed the link above
It still did not help me
function ValidateMandatoryFields() {
alert("entered into the function");
var inputControls = document.getElementsByTagName("input");
alert(inputControls.length);
for (var i = 0; i < inputControls.length; i++) {
if (inputControls[i].getAttribute("IsKeyField") == "Y") {
alert(inputControls[i]);
}
else {
alert("first if statemenet");
}
if (inputControls[i].getAttribute("Type") == "T" || (inputControls[i].getAttribute("Type") == "C")) {
alert(inputControls[i]);
}
else {
alert("second if statement");
}
if (inputControls[i].getAttribute("IsMandatory") == "Y") {
alert(inputControls[i]);
}
else {
alert("third if statement");
}
if (inputControls[i].value == "") {
alert(inputControls[i].getAttribute("KeyField_Name") + "is required.");--error here
errorMsg += "\n" + inputControls[i].getAttribute("KeyField_Name") + " is required.";
isValidated = false;
}
else {
alert("name: " + inputControls[i].id);
alert("length: " + inputControls[i].value.length());
alert("value: " + inputControls[i].value);
}
}
}
It pop ups of all else statements appear and finally i get a error stating Microsoft JScript runtime error: Object expected. Which means that none of my if condition is true...How do I get it to work...please help me with the correct logic