Checking input fields(text) value when field is loosing focus

J

JohnnyW

Hello,

I have I simple question that is not clear for me.
So, I have a input field (text field) in my html page.
User should give input as a string like following 12 345 678,0

If user gives input in format 12345678,0 and leves the input field
how can I format the value 12345678,0 into format 12 345 678,0?

Are there any Javascript that can handle this with Regular Expressions?

Thanks!
 
T

Thomas 'PointedEars' Lahn

JohnnyW said:
[...] I have a input field (text field) in my html page.
User should give input as a string like following 12 345 678,0

If user gives input in format 12345678,0 and leves the input field
how can I format the value 12345678,0 into format 12 345 678,0?

Are there any Javascript that can handle this with Regular Expressions?

Do something like the following when the blur event of that control occurs:

var s = "12345678,0";
var rx = /(\d)((\d{3})+,\d+)$/;
while (rx.test(s = s.replace(rx, "$1 $2"))) ;

// just for debugging
window.alert(s);

I do not think it can be done without the loop, but I would be glad to be
proven wrong.


PointedEars
 
R

Randy Webb

Thomas 'PointedEars' Lahn said the following on 3/17/2006 11:50 PM:
JohnnyW said:
[...] I have a input field (text field) in my html page.
User should give input as a string like following 12 345 678,0

If user gives input in format 12345678,0 and leves the input field
how can I format the value 12345678,0 into format 12 345 678,0?

Are there any Javascript that can handle this with Regular Expressions?

Do something like the following when the blur event of that control occurs:

No, you do something like that when the onchange event is fired, not
onblur as onblur causes the code to be needlessly executed every time
the field is blurred.
 
T

Thomas 'PointedEars' Lahn

Randy said:
Thomas 'PointedEars' Lahn said the following on 3/17/2006 11:50 PM:

No, you do something like that when the onchange event is fired, not
onblur as onblur causes the code to be needlessly executed every time
the field is blurred.

True, my bad.


PointedEars
 
B

BootNic

JohnnyW said:
Hello,

I have I simple question that is not clear for me.
So, I have a input field (text field) in my html page.
User should give input as a string like following 12 345 678,0

If user gives input in format 12345678,0 and leves the input field
how can I format the value 12345678,0 into format 12 345 678,0?

Are there any Javascript that can handle this with Regular
Expressions?

Thanks!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
function filter(x){
var s = x.value;
s=s.replace(/\D+/g,'');
if(s.length==9){
s=s.replace(/(\d{2})(\d{3})(\d{3})(\d)/,'$1 $2 $3,$4');
x.value=s;
}
else{
alert('incorrect input');
x.select();
setTimeout(function(){x.focus();},0);
}
}
</script>
<title></title>
</head>
<body>
<input type="text" maxlength="12" onchange="filter(this)">
</body>
</html>

--
BootNic Saturday, March 18, 2006 9:55 AM

When I was young, I was put in a school for retarded kids for two
years before they realized I actually had a hearing loss...and they
called ME slow!
*Kathy Buckley*
 
J

JohnnyW

Thank you for all of you guys!


BootNic said:
Hello,

I have I simple question that is not clear for me.
So, I have a input field (text field) in my html page.
User should give input as a string like following 12 345 678,0

If user gives input in format 12345678,0 and leves the input field
how can I format the value 12345678,0 into format 12 345 678,0?

Are there any Javascript that can handle this with Regular
Expressions?

Thanks!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
function filter(x){
var s = x.value;
s=s.replace(/\D+/g,'');
if(s.length==9){
s=s.replace(/(\d{2})(\d{3})(\d{3})(\d)/,'$1 $2 $3,$4');
x.value=s;
}
else{
alert('incorrect input');
x.select();
setTimeout(function(){x.focus();},0);
}
}
</script>
<title></title>
</head>
<body>
<input type="text" maxlength="12" onchange="filter(this)">
</body>
</html>

--
BootNic Saturday, March 18, 2006 9:55 AM

When I was young, I was put in a school for retarded kids for two
years before they realized I actually had a hearing loss...and they
called ME slow!
*Kathy Buckley*
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 17 Mar
2006 21:34:05 remote, seen in JohnnyW
I have I simple question that is not clear for me.
So, I have a input field (text field) in my html page.
User should give input as a string like following 12 345 678,0

If user gives input in format 12345678,0 and leves the input field
how can I format the value 12345678,0 into format 12 345 678,0?

Are there any Javascript that can handle this with Regular Expressions?

<URL:http://www.merlyn.demon.co.uk/js-index.htm#RecTS> combined with
..replace(/\./g, ",") .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top