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

Validating form

P: 51
Hi
i dont have much idea in javascript.i have a web application where i would like to validate a text field. user will only be able to insert floating number (like 4.5, 10.45). no letter will be allowed.

please assit.

Best regards
Mainul
Feb 8 '07 #1
Share this Question
Share on Google+
9 Replies

dmjpro
100+
P: 2,476
try thsi code in js
function validate()
{
// to access text value do ======= document.all.textfield_name.value
}
plz send me what u want to do
i am online
Feb 8 '07 #2

acoder
Expert Mod 15k+
P: 16,027
If you know regular expressions, use the following for floating point number:
Expand|Select|Wrap|Line Numbers
  1. /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/
then test:
Expand|Select|Wrap|Line Numbers
  1. var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/
  2. if (!reFloat.test(yourformfield)) alert("Error: not a floating point number")
Feb 8 '07 #3

acoder
Expert Mod 15k+
P: 16,027
try thsi code in js
function validate()
{
// to access text value do ======= document.all.textfield_name.value
}
plz send me what u want to do
i am online
document.all is old code and IE-specific. You should use document.getElementById("textfieldid").value instead.
Feb 8 '07 #4

dmjpro
100+
P: 2,476
sorrryyyyyyyyyyyyyyy
without reading the problem i responded...
u can stop user to press alphabatic word...
try this code ....
<input type = text onkeypress = test()>

function test()
{
if(!(event.keyCode>=49&&event.keyCode<=57)) event.keyCode = 0;
if(event.keyCode == point_keycode && event.srcElement.value.indexOf('.') == -1) event.keyCode = 0;
}

to get the point_keycode try this code ...
<body onkeypress = test()>
function test()
{
alert(event.keyCode);
}

plz press any key u want to know it's keyCode..
plz maintain the case sensivity while u writing the code..
any problem while u write the code plz send me ur problem.....
i am online
Feb 8 '07 #5

P: 51
thanks a lot guys for quick response. let me try the codes.

best regards
Mainul
Feb 8 '07 #6

dmjpro
100+
P: 2,476
ok try it quickly and send me ur result
i am eager to know the result
Feb 8 '07 #7

100+
P: 271
sorrryyyyyyyyyyyyyyy
without reading the problem i responded...
u can stop user to press alphabatic word...
try this code ....
<input type = text onkeypress = test()>

function test()
{
if(!(event.keyCode>=49&&event.keyCode<=57)) event.keyCode = 0;
if(event.keyCode == point_keycode && event.srcElement.value.indexOf('.') == -1) event.keyCode = 0;
}

to get the point_keycode try this code ...
<body onkeypress = test()>
function test()
{
alert(event.keyCode);
}

plz press any key u want to know it's keyCode..
plz maintain the case sensivity while u writing the code..
any problem while u write the code plz send me ur problem.....
i am online
hi dmjpro,
the coding is good. can u get me the key code for special keys like *,-,+ etc.
Feb 8 '07 #8

dmjpro
100+
P: 2,476
can't u get it from my code.....
Feb 8 '07 #9

acoder
Expert Mod 15k+
P: 16,027
To get the event code across all browsers and not just IE, use
Expand|Select|Wrap|Line Numbers
  1. function setEvent(Id) {
  2.  Id.onkeypress = function keyPress(evt) { 
  3.   if (evt) 
  4.    keyCode = evt.keyCode ;
  5.   else  
  6.    keyCode = event.keyCode ;
  7.  alert(keyCode);
  8.  }
  9. }
http://www.bigbold.com/snippets/posts/show/309
Feb 8 '07 #10

Post your reply

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