I have to design a UI where i have multiple fields. I need to validate them if they are null. For two fields [App Port for Release 1 and App Port for Release 3], i need them to start with 4 and 6 respectively and 4 digits in length.
Please find the code. For some reason it is not working.
Also i have a table which on clicking ‘Add Row button’ an addtional row is cretaed. But I was not able to increment the row count for every increment in row and re-adjustement in row count when any of the rows is deleted.
<!DOCTYPE html>
<html>
<head>
<SCRIPT TYPE=”text/javascript”>
function Validate()
{
var valid = true;
var msg=”Incomplete form:n”;
var count = 0;
var x1 = document.sform.app_name.value;
var x21 = document.sform.app_port1.value;
var x22 = document.sform.app_port2.value;
var x31 = document.sform.Work_dir1.value;
var x32 = document.sform.Work_dir2.value;
var x4 = document.sform.App_Log.value;
var x5 = document.sform.Test_URL.value;
var x6 = document.sform.Strt_Log.value;
var x7 = document.sform.Stop_Log.value;
var x8 = document.sform.Strt_Time.value;
var x9 = document.sform.Stop_Time.value;
if ( x1 == “” || x1 == null)
{
msg+=”You need to fill the name field!n”;
valid = false ;
}
if ( x21.length != 0 )
{
if (valid)
{
if(isNaN(x21)|| x.indexOf(” “)!=-1)
{
alert(“Enter numeric value that starts from 4 for App Port – Release 1”);valid = false;
return valid; }
else if ( x21.length != 4 ){
alert(“enter 4 characters”); valid = false;
return valid; }
else if (x21.charAt(0)!=”4″ ){
alert(“it should start with 4 “);
valid = false;
return valid; }
}
}
else
{
msg+=”You need to fill in App Port for Release 1!n”;
valid = false; }
if ( x22.length != 0 )
{
if (valid)
{
if(isNaN(x22)|| x.indexOf(” “)!=-1)
{
alert(“Enter numeric value that starts from 6 for App Port – Release 3”);valid = false;
return valid; }
else if ( x22.length != 4 ){
alert(“enter 4 characters”); valid = false;
return valid; }
else if (x22.charAt(0)!=”6″ ){
alert(“it should start with 6 “);
valid = false;
return valid; }
}
}
else
{
msg+=”You need to fill in App Port for Release 3!n”;
valid = false; }
if ( x31 == “” || x31 == null)
{
msg+=”You need to fill the Work Directory For Release 1!n”;
valid = false ;
}
if ( x32 == “” || x32 == null)
{
msg+=”You need to fill the Work Directory For Release 3!n”;
valid = false ;
}
if ( x4 == “” || x4 == null)
{
msg+=”You need to fill the App LOG !n”;
valid = false ;
}
if ( x5 == “” || x1 == null)
{
msg+=”You need to fill the Test URL !n”;
valid = false ;
}
if ( x6 == “” || x1 == null)
{
msg+=”You need to fill the Start up log!n”;
valid = false ;
}
if ( x7 == “” || x1 == null)
{
msg+=”You need to fill the Stop log !n”;
valid = false ;
}
if ( x8 == “” || x1 == null)
{
msg+=”You need to fill the Start Time out!n”;
valid = false ;
}
if ( x9 == “” || x1 == null)
{
msg+=”You need to fill the Stop Time out!n”;
valid = false ;
}
if (!valid) alert(msg);
return valid;
}
var count = “1”;
function addRow(in_tbl_name)
{
var tbody = document.getElementById(in_tbl_name).getElementsByTagName(“TBODY”)[0];
// create row
var row = document.createElement(“TR”);
// create table cell 1
var td1 = document.createElement(“TD”)
var strHtml1 = “<FONT SIZE=”+3″>*</FONT>”;
td1.innerHTML = strHtml1.replace(/!count!/g,count);
// create table cell 2
var td2 = document.createElement(“TD”)
var strHtml2 = “<INPUT TYPE=”text” NAME=”in_name” SIZE=”30″ MAXLENGTH=”30″ STYLE=”height:24;border: 1 solid;margin:0;”>”;
td2.innerHTML = strHtml2.replace(/!count!/g,count);
// create table cell 3
var td3 = document.createElement(“TD”)
var strHtml3 = “<INPUT TYPE=”text” NAME=”in_name” SIZE=”30″ MAXLENGTH=”30″ STYLE=”height:24;border: 1 solid;margin:0;”>”;
td3.innerHTML = strHtml3.replace(/!count!/g,count);
// create table cell 4
var td4 = document.createElement(“TD”)
var strHtml4 = “<INPUT TYPE=”text” NAME=”in_name” SIZE=”30″ MAXLENGTH=”30″ STYLE=”height:24;border: 1 solid;margin:0;” READONLY>”;
td4.innerHTML = strHtml4.replace(/!count!/g,count);
// create table cell 5
var td5 = document.createElement(“TD”)
var strHtml5 = “<INPUT TYPE=”Button” CLASS=”Button” onClick=”delRow()” VALUE=”Delete Row”>”;
td5.innerHTML = strHtml5.replace(/!count!/g,count);
// append data to row
row.appendChild(td1);
row.appendChild(td2);
row.appendChild(td3);
row.appendChild(td4);
row.appendChild(td5);
// add to count variable
count = parseInt(count) + 1;
// append row to table
tbody.appendChild(row);
}
function delRow()
{
var current = window.event.srcElement;
//here we will delete the line
while ( (current = current.parentElement) && current.tagName !=”TR”);
current.parentElement.removeChild(current);
}
</SCRIPT>
</head>
<body>
<form name=”sform” method=”post” action=”
<table>
<tr>
<TD valign=”top” class=”borderB” height=”25″>
App Name
<font class=starmand align=”absmiddle”>*</font>
</TD>
<TD align=left class=”borderB” colspan=”2″ height=”25″>
<input type=”text” name=”app_name” id=”app_name” maxlength=”30″ size=”35″ >
</td>
</tr>
<TR>
<TD valign=”top” class=”borderB” height=”25″>
App Port
<font class=starmand align=”absmiddle”>*</font>
</TD>
<TD align=left class=”borderB” colspan=”2″ height=”25″>
<input type=”text” name=”app_port1″ id=”app_port1″ maxlength=”30″ size=”35″ >
<span><font color=#999999>(release 1)</font></span>
</td></tr>
<tr> <td class=”borderB” height=”25″> </td><td colspan=”2″ class=”borderB”>
<input type=”text” name=”app_port2″ id=”app_port2″ maxlength=”30″ size=”35″ >
<span><font color=#999999>(release 3)</font></span>
</td></tr>
<TR>
<TD valign=”top” class=”borderB” height=”25″>
Work Directory
<font class=starmand align=”absmiddle”>*</font>
</TD>
<TD align=left class=”borderB” colspan=”2″ height=”25″>
<input type=”text” name=”Work_dir1″ id=”Work_dir1″ maxlength=”30″ size=”35″ >
<span><font color=#999999>(release 1)</font></span>
</td></tr>
<tr> <td class=”borderB” height=”25″> </td><td colspan=”2″ class=”borderB”>
<input type=”text” name=”Work_dir1″ id=”Work_dir1″ maxlength=”30″ size=”35″ >
<span><font color=#999999>(release 3)</font></span>
</td></tr>
<tr>
<TD valign=”top” class=”borderB” height=”25″>
App LOG
<font class=starmand align=”absmiddle”>*</font>
</TD>
<TD align=left class=”borderB” colspan=”2″ height=”25″>
<input type=”text” name=”App_Log” id=”App_Log” maxlength=”30″ size=”35″ >
</td>
</tr>
<tr>
<TD valign=”top” class=”borderB” height=”25″>
Test URL
<font class=starmand align=”absmiddle”>*</font>
</TD>
<TD align=left class=”borderB” colspan=”2″ height=”25″>
<input type=”text” name=”Test_URL” id=”Test_URL” maxlength=”30″ size=”35″ >
</td>
</tr>
<tr>
<TD valign=”top” class=”borderB” height=”25″>
Start up log
<font class=starmand align=”absmiddle”>*</font>
</TD>
<TD align=left class=”borderB” colspan=”2″ height=”25″>
<input type=”text” name=”Strt_Log” id=”Strt_Log” maxlength=”30″ size=”35″ >
</td>
</tr>
<tr>
<TD valign=”top” class=”borderB” height=”25″>
Stop Log
<font class=starmand align=”absmiddle”>*</font>
</TD>
<TD align=left class=”borderB” colspan=”2″ height=”25″>
<input type=”text” name=”Stop_Log” id=”Stop_Log” maxlength=”30″ size=”35″ >
</td>
</tr>
<tr>
<TD valign=”top” class=”borderB” height=”25″>
Start up time out
<font class=starmand align=”absmiddle”>*</font>
</TD>
<TD align=left class=”borderB” colspan=”2″ height=”25″>
<input type=”text” name=”Strt_Time” id=”Strt_Time” maxlength=”30″ size=”35″ >
</td>
</tr>
<tr>
<TD valign=”top” class=”borderB” height=”25″>
Stop time out
<font class=starmand align=”absmiddle”>*</font>
</TD>
<TD align=left class=”borderB” colspan=”2″ height=”25″>
<input type=”text” name=”Stop_Time” id=”Stop_Time” maxlength=”30″ size=”35″ >
</td>
</tr>
<TR>
<TD class=”borderB” colspan=”3″ height=”25″><h3>Sub Apps</h3></TD>
</TR>
<TABLE ID=”tblPets” border=”1″ STYLE=”border width:1 orange dashed;background color:#F0E68C;table-row width:2;”>
<TR><TH>Rownum</TH><TH WIDTH=”70″>App Name</TH><TH WIDTH=”230″>Start Time Line</TH><TH WIDTH=”230″>Stop Time Line</TH><TD ALIGN=”center”><INPUT TYPE=”Button” onClick=”addRow(‘tblPets’)” VALUE=”Add Row”></TD></TR>
</TR>
</TABLE>
</table>
<tr>
<input type=”submit” value=”submit” name=”Submit” onclick=”return Validate()”/>
</form>
<tr>
</body>
</html>
Please help me.
Thanks in advance,
Divya