By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,817 Members | 2,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,817 IT Pros & Developers. It's quick & easy.

Javascript-HTML elements

darksteel21
P: 36
Hi to all,

Im seeking for a solution for my problem in javascript..how can i get the values of an unspecified number of html textfields using javascript??

The scenario is this:
The user will specify the number of fields he wants, for example he specified 3 textfields,then my script will generate 3 html textfields..i want to validate these three textfields before submitting the form,it should not contain NULL values..how should i do this?? hope someone could help me...
Jul 24 '08 #1
Share this Question
Share on Google+
4 Replies


10K+
P: 13,264
Write a generic validator that takes any input field and validates it according to your requirements. You then call that finction on each of your text boxes.
Jul 24 '08 #2

darksteel21
P: 36
what do you mean by generic validator?please pardon me because im not that proficient in javascript...
But I have a script here to get the values of Html textfields:
[html]
<html>
<body>
<script language="JavaScript">
function function1() {
var m = document.getElementsByName("myE");
alert(m.length);
alert(m[0].name + "/" + m[0].value);
alert(m[1].name + "/" + m[1].value);
}
</script>

<form name="form1">
<input type="text" name="mytxt1" id="myE" value="">
<input type="text" name="mytxt2" id="myE" value="">
<input type="button"
value="Get the number of elements that have the same name"
onClick="function1();">
</form>
</body>
</html>
[/html]
it works fine on IE browsers..but not on firefox...
when you browse it on IE it detects 2 textfields, but on Firefox it only detects nothing...can you please explain me this problem?
Jul 25 '08 #3

rnd me
Expert 100+
P: 427
ids need to be different. names must be the same to use doc.getbyname
Jul 25 '08 #4

RamananKalirajan
100+
P: 607
Hi Dude, I hope this code will help you. But you have to enhance yourself to your need.

[HTML]<html>
<head>
<script language="javascript">
var choice;
function createText()
{
choice=parseInt(document.getElementById('user').va lue);
var textId;
for(var i=1;i<=choice;i++)
{
textId = "textId"+i;
var udiv=document.getElementById('myDiv')
var newEl=document.createElement('input');
newEl.type='text';
newEl.id = textId;
udiv.appendChild(newEl);
}
}

function validateText()
{
var textId
for(var i=1;i<=choice;i++)
{
textId = "textId"+i;
var temp = document.getElementById(textId).value;
if(temp=="")
alert('text '+i+' is null');
}
}
</script>
</head>
<div id='myDiv'>
</div>
<br/>
<input type="text" id='user'>
<br/>
<input type="Button" value="generate" onclick="createText()"><br/>
<input type="button" value="validate" onclick="validateText()">
</html>[/HTML]

Pls Post back your feedback. If you need more help i can help you out

Regards
Ramanan Kalirajan
Jul 28 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.