HighDots Forums  

Numeric field validation fails for single character

Javascript JavaScript language (comp.lang.javascript)


Discuss Numeric field validation fails for single character in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
nkle004@gmail.com
 
Posts: n/a

Default Numeric field validation fails for single character - 03-03-2006 , 01:03 AM






I am using following function to check for my input
my input is like 0.00 i dont want to allow any specialcharacters.

function numericValidation() {
var x = this;
var reg = new RegExp('^[0-9]*\.?[0-9]*$');

if (reg.test(x)) {
return true;
}
else return false;
}

here problem is when i type one character other than this range Regular
expression doesnt work i mean it gives true for input '@' if i type
'@@'then it gives false

actually it shd gv false for both the case.


Reply With Quote
  #2  
Old   
RobG
 
Posts: n/a

Default Re: Numeric field validation fails for single character - 03-03-2006 , 01:17 AM






nkle004 (AT) gmail (DOT) com wrote:
Quote:
I am using following function to check for my input
my input is like 0.00 i dont want to allow any specialcharacters.

function numericValidation() {
var x = this;
var reg = new RegExp('^[0-9]*\.?[0-9]*$');
---------------------------------------------^

When calling RegExp as a constructor, quoted special characters in the
string literal need two backslashes - you have to quote the quote to get
it past the parser (if that makes sense...). Use:

var reg = new RegExp('^[0-9]*\\.?[0-9]*$');


Note use of '\\'.


[...]


--
Rob


Reply With Quote
  #3  
Old   
Vic Sowers
 
Posts: n/a

Default Re: Numeric field validation fails for single character - 03-03-2006 , 02:42 AM




"RobG" <rgqld (AT) iinet (DOT) net.au> wrote

Quote:
nkle004 (AT) gmail (DOT) com wrote:
I am using following function to check for my input
my input is like 0.00 i dont want to allow any specialcharacters.

function numericValidation() {
var x = this;
var reg = new RegExp('^[0-9]*\.?[0-9]*$');
---------------------------------------------^

When calling RegExp as a constructor, quoted special characters in the
string literal need two backslashes - you have to quote the quote to get
it past the parser (if that makes sense...). Use:

var reg = new RegExp('^[0-9]*\\.?[0-9]*$');


Note use of '\\'.

Or use a regular expression literal:
var reg = /^[0-9]*\.?[0-9]*$/;

Quote:
[...]


--
Rob



Reply With Quote
  #4  
Old   
nkle004@gmail.com
 
Posts: n/a

Default Re: Numeric field validation fails for single character - 03-03-2006 , 03:26 AM



if i use a regular expression literal can i still use test methos with
it ?:
Quote:
var reg = /^[0-9]*\.?[0-9]*$/;
Vic Sowers wrote:
Quote:
"RobG" <rgqld (AT) iinet (DOT) net.au> wrote in message
news:0cRNf.599$Nv4.93706 (AT) news (DOT) optus.net.au...
nkle004 (AT) gmail (DOT) com wrote:
I am using following function to check for my input
my input is like 0.00 i dont want to allow any specialcharacters.

function numericValidation() {
var x = this;
var reg = new RegExp('^[0-9]*\.?[0-9]*$');
---------------------------------------------^

When calling RegExp as a constructor, quoted special characters in the
string literal need two backslashes - you have to quote the quote to get
it past the parser (if that makes sense...). Use:

var reg = new RegExp('^[0-9]*\\.?[0-9]*$');


Note use of '\\'.


Or use a regular expression literal:
var reg = /^[0-9]*\.?[0-9]*$/;


[...]


--
Rob


Reply With Quote
  #5  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Numeric field validation fails for single character - 03-03-2006 , 09:46 AM



nkle004 (AT) gmail (DOT) com wrote:

Quote:
if i use a regular expression literal can i still use test methos with
it ?:
var reg = /^[0-9]*\.?[0-9]*$/;
Yes, you can. The RegExp literal creates a RegExp object. Why do you not
just try it?

You can even provide attribution of quoted material, and stop top-posting.

<URL:http://safalra.com/special/googlegroupsreply/>


PointedEars


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.