469,575 Members | 1,608 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,575 developers. It's quick & easy.

Validating form

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
9 1188
dmjpro
2,476 2GB
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
16,027 Expert Mod 8TB
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
16,027 Expert Mod 8TB
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
2,476 2GB
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
mainul
51
thanks a lot guys for quick response. let me try the codes.

best regards
Mainul
Feb 8 '07 #6
dmjpro
2,476 2GB
ok try it quickly and send me ur result
i am eager to know the result
Feb 8 '07 #7
karthi84
271 100+
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
2,476 2GB
can't u get it from my code.....
Feb 8 '07 #9
acoder
16,027 Expert Mod 8TB
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.

Similar topics

reply views Thread by Bradley Bossard via DotNetMonster.com | last post: by
2 posts views Thread by Chris Dunaway | last post: by
reply views Thread by Gary Shell | last post: by
4 posts views Thread by easoftware | last post: by
1 post views Thread by =?Utf-8?B?bGpsZXZlbmQy?= | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.