0

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

Community
  • 1
  • 1
Janet
  • 1,391
  • 14
  • 40
  • 62

2 Answers2

0

Try adding an else clause to see what the values are:

if (inputControls[i].value == "") {
   ... your code
} else
{
   alert("name: " + inputControls[i].id);
   alert("length: " + inputControls[i].value.length());
   alert("value: " + inputControls[i].value);
}

It's possible they are set to null or spaces instead of just an empty string.

jColeson
  • 951
  • 1
  • 14
  • 24
  • @JColeson, i have added the code as you suggested, it still does not give any alert for name, length & value ...Dont know what am I missing – Janet Apr 18 '11 at 19:27
  • What does it display for: alert(inputControls.length); And does your browser say if are there any script errors on the page? – jColeson Apr 18 '11 at 19:33
  • 89 for inputControls.Length, and there are no errors on the browser. – Janet Apr 18 '11 at 19:42
  • 1
    Add else clauses to all the other if statements and see which If statement is returning false. – jColeson Apr 18 '11 at 19:44
  • I have edited my code above with the else statements and the error message, ...help me with the correct logic... – Janet Apr 18 '11 at 20:03
  • Change: alert("first if statemenet"); to alert("first if statemenet" + inputControls[i].getAttribute("IsKeyField")); – jColeson Apr 18 '11 at 20:19
  • I get this error Microsoft JScript runtime error: Object expected – Janet Apr 18 '11 at 20:38
  • This is where i get the error. – Janet Apr 18 '11 at 20:45
0

I was able to fix my code as shown below:

function validateInput() {
var arrTextBox = document.getElementsByTagName("input");
var retValue = 1;
var returnValue = 1;
for (i = 0; i < arrTextBox.length; i++) {
    if (arrTextBox[i].type =="text" && arrTextBox[i].getAttribute("IsMandatory")=="Y" && arrTextBox[i].value == "") {
        retValue = 0;
    }
}
if (retValue == 0) {
    alert("Validation Failed");
    return false;
}
else {
    alert("Validation Success");
    return true;
}

on the ascx.cs file

 btnAddItem.Attributes.Add("onclick", "{return validateInput()};");
Janet
  • 1,391
  • 14
  • 40
  • 62