formatting a textbox with a specific format

fmh002
fmh002 used Ask the Experts™
on
hello,

i have a sort of validation that i need to implement in my form.

there are textboxes that should accept only numbers. as the user types the appropriate commas should appear accordingly.

example: the user types 25000 but in the text box it appears as 25,000 automatically.

i dont want the user to input points as there wont be any decimals in the values. he/she should only input the number.

could i get some help on it with javascript?

peace,
fmh002
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Basically you would have a function that gets fired onKeyPress that firstly checks the user only types numbers and second checks the length and if the length is 3/6/9, etc then it sticks a ',' in the string.

This is a pretty complicated piece of javascript for only 30 points, however this is one of the best javascript sites I have come across: http://www.dynamicdrive.com that may help your cause.
<html>
<head>
<title></title>
<script type="text/javascript">
<!--
function formatZip(objFormField){
  intFieldLength = objFormField.value.length;
  if(intFieldLength==2){
       objFormField.value = objFormField.value + ",";
            return false;
  }
}
//-->
</script>
</head>
<body>
<form name="form1" method="post" action="">
  <input type="text" name="zip" onKeyPress="formatZip(this)" maxlength="6">
</form>
</body>
</html>
disregard the function name

<script type="text/javascript">
<!--
function formatField(objFormField){
  intFieldLength = objFormField.value.length;
  if(intFieldLength==2){
       objFormField.value = objFormField.value + ",";
            return false;
  }
}
//-->
</script>
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

 <input type="text" name="zip" onKeyPress="formatField(this)" maxlength="6">
oooppppssss

  <input type="text" name="numField" onKeyPress="formatField(this)" maxlength="6">

Author

Commented:
thnx for the link rdmjrb, i would have given it 60 points but im out of points right now.

jaysolomon!!! sup? i see you got what i wanted. your script works fine with maxlength=6. in my environment it is a bit difficukt coz the digits always exceed the max length=6. i live in a country where a dollar costs a 1000 shillings.

i would like to go to a maximum of 9. could you help me with the commas? it's working fine right now coz in my question i gave a 6 digit example. i see my mistake.

thnx once again jaysolomon

fmh002

Author

Commented:
i've also noticed that if i type in 2500 it gives it a comma as well -> 25,00... ooops.

Author

Commented:
i'd also like have only numbers entered. at the moment there is no restriction on the input in your script.

peace,
fmh002
here is a link that may be very helpful.

Try it out and if you still need help just let me know. I will continue to write a shorter script.

jAy
sh!t i forgot the link brb....
http://www.mredkj.com/javascript/numberFormat.html#tryit

there is the link

after you look at that and if you decide to go that route, we can then see your form and create the Number Only function

Author

Commented:
sup jaysolomon

wow that script on that page is quite complex and trying to customize it for my form is way beyond my capabilities. but there's a setback, that try it button does the formatting. i am sure there must be a way to make that function trigger onkeypress or something? dont you think so? well, i have decided to go that route but that button shouldn't do the function, it should be done on the textbox itself.

peace
fmh002

Author

Commented:
that script has many functions, could you make it shorter? do only the comma placements

Author

Commented:
people i need help, i've increased points for that matter...

peace
fmh002
is this going to be a currency format, or do you just want a comma

also is it always 5 numbers or what is the max amount of numbers

Author

Commented:
i just want a comm and my max amount of numbers could be 9,999,999
try this

<html>
<head>
<title></title>
<script type="text/javascript">
<!--
function addCommas() {
var x = document.forms[0];
var objRegExp  = new RegExp('(-?[0-9]+)([0-9]{3})');
    while(objRegExp.test(x.fieldName.value)) {
       x.fieldName.value = x.fieldName.value.replace(objRegExp, '$1,$2');
    }
}
//-->
</script>
</head>

<body>
<form name="form1" method="post" action="">
  <input type="text" name="fieldName" onChange="return addCommas();">
  <input type="text" name="textfield">
</form>
</body>
</html>
i tried using onKeyPress, but it got screwed up

Author

Commented:
thnx jaysolomon,

now i can atleast say that look, the commas do appear!

thnx man,
fmh002
Uh why the grade of a "C"?

You asked for commas, thats what you got

Author

Commented:
hey man im sorry for grading a C on that. i'd make it up to you if you could help me with some other problem i got.
you can post a Question in Community Support asking them to change this grade.

Now whats the Problem?
Response.Write "<SELECT name=studentid onchange=""studentname.value = this.value;"">"


I think it should be like so

Response.Write "<SELECT name=studentid onchange='this.form.studentname.value = this.value;'>"

and

Response.Write "<SELECT name=studentname onchange='this.form.studentid.value = this.value;'>"


This is contredicting itself to me

and this is why you are getting the same results in the 2 DB fields

Author

Commented:
i have already posted on the community support, jay am i ok with u now?
no problem man

BTW, i don't know how the mods or admins will act when they  see the link to the other forum, so wait and see.

Did you understand the post about the problem

Author

Commented:
i tried changing the code to the one you gave and it's still posting the same value (studentid) to both of the fields

Author

Commented:
i am trying what that mohecan guy suggested, let me c if that works
Grade will be changed from C to A per fmh002 request.

YensidMod
EE Moderator
:>)

thanks

Author

Commented:
jay it's working now. i tried chaging the code to what mohecan suggested and the posting's fine now.

im sorry for the points thing man. thnx
No problem

Happy Programming

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial