PDA

View Full Version : validation many text box same name?



satkannanab
07-29-2008, 03:53 AM
Hi......
Hello experts please give me a sample code for validating many text box with same.i knew for validate a number but i couldnt get how to validate for many text box with same name.

regards
satheesh kannan

jscheuer1
07-29-2008, 05:41 AM
Since you haven't made it clear what we are validating on, you will have to make up your own rule for the highlighted part. Or you could tell me what we are validating on, and I could tell you what to put there:


<script type="text/javascript">
function val_many(f, n){
for (var e = f.elements, i = e.length - 1; i > -1; --i)
if (e[i].type && e[i].type == 'text' && e[i].name && e[i].name == n && !(e[i].value something)){
alert('Some Message');
e[i].focus();
return false;
}
return true;
}
</script>

Then in the opening tag of your form:


<form action="whatever" onsubmit="return val_many(this, 'some_name');">

satkannanab
07-29-2008, 06:48 AM
Hi..
Thanks for your kind reply.
I need to validate some conditions .
They are...1.not to be empty field,2.Only numerical,3.Fixed decimal of 4 digits only. These were the validation i have to do.

return val_many(this, 'some_name');">
is it some_name should be text box name or wat?.

Thanks in advance,
regards,
satheesh kannan

jscheuer1
07-29-2008, 07:26 AM
I'm not clear on 3:


Fixed decimal of 4 digits only

Would:

1

be OK?

How about:

1.352

?

What would be an example of the smallest number allowed? The largest? Is it that if decimals are used, only 4 places are allowed, or are 4 decimal places required?

To answer your question, yes. You would replace some_name with the name of your text fields.

satkannanab
07-29-2008, 08:45 AM
3. point is to fixed maximum number of 4 digits only e.g: 1234
No fraction s are allowed like 1.234.


satheesh

jscheuer1
07-29-2008, 12:15 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function val_many(f, n){
for (var e = f.elements, i = e.length - 1; i > -1; --i)
if (e[i].type && e[i].type == 'text' && e[i].name && e[i].name == n && (e[i].value == ''
|| isNaN(e[i].value - 0) || /\./.test(e[i].value) || e[i].value > 9999)){
alert('Some Message');
e[i].focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form action="#" onsubmit="return val_many(this, 'some_name');">
<input type="text" name="some_name"><br>
<input type="text" name="some_name"><br>
<input type="text" name="some_name"><br>
<input type="text" name="some_name"><br>
<input type="submit" value="Go!">
</form>
</body>
</html>

satkannanab
07-29-2008, 01:18 PM
Hi......
Thanks for ur kind and wonderful reply.
Its alerting when the function encounters a problem,but after clicking ok
in alert box the page getting load to next page.Its not focussing to the corresponding text box.

I think the function is returning but not returning false;

Waiting for ur reply,

regards,
satheesh kannan

satkannanab
07-29-2008, 01:20 PM
Thanks yar...
Its working fine.i just taken out the
e[i].focus(); this code.
But again i have a small problem.some text boxes are disabled in the same name.
If i filled out correctly for all enabled text box,its being alerting for disabled text box too.HOw i can cure this too.


Regards,
satheesh kannan

jscheuer1
07-29-2008, 02:59 PM
Here (addition red):


if (e[i].type && e[i].type == 'text' && e[i].name && e[i].name == n && !e[i].disabled && (e[i].value == ''
|| isNaN(e[i].value - 0) || /\./.test(e[i].value) || e[i].value > 9999)){

satkannanab
07-30-2008, 08:01 AM
Hi......
Its alerting for all text boxes if its enabled or disabled.If i filled out all text boxs also it alerting the message.




Waiting for ur reply,

regards,
satheesh kannan

satkannanab
07-30-2008, 08:52 AM
All are working fine.
Thanks for ur replies.

regards
satheesh kannan