Solved

validate if a textbox in a datagrid allows any special symbols other than "|"(pipe)  to be entered using javascript

Posted on 2008-10-28
11
374 Views
Last Modified: 2012-05-05
Hi!

I have a datagrid.I have a textbox in one of the columns  in the datagrid.
The textbox should allow numbers and  a "|". it should not allow any special characters other than pipe.(|)
The validation ha sto be done using javascript.(not by using validation controls)
I need it ASAP.

IT'S EXTREMELY URGENT.

CAN I GET THE CODE/LOGIC FOR THAT asap.

thanks  a million in advance.
0
Comment
Question by:amukta
  • 6
  • 5
11 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22828751
try:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title></title>
<script type="text/javascript">
function validate(o){
	if( /[^\d|]/.test(o.value) )
	{
		alert("Invalid char found. Only digits and | are allowed.");
		setTimeout(function(){focusIt(o)},10);
	return false;
	}
return true;
}
function focusIt(o){
	o.focus();
}
</script>
</head>
<body>
<input type="text" onchange="validate(this)"/>
</body>
</html>

Open in new window

0
 

Author Comment

by:amukta
ID: 22828908
it is still allowing to insert "|"(pipe.)
0
 
LVL 82

Expert Comment

by:hielo
ID: 22828914
I know. You said:
>> it should not allow any special characters other than pipe.(|)

So, what is it you want?
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:amukta
ID: 22828915
what is test(o.value)?
0
 

Author Comment

by:amukta
ID: 22828921
I need it ASAP.
0
 

Author Comment

by:amukta
ID: 22828922
i NEED IT asap.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22828950
>>what is test(o.value)?
o.value is the value of the text box

>>I need it ASAP
And if you don't clarify what you want, you are just delaying the process. I gave you EXACTLY what you asked for. So, if you mistyped the question you need to clarify.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22828953
If you want ONLY digits then use:
if( /[^\d]/.test(o.value) )
0
 

Author Comment

by:amukta
ID: 22836136
Is test(o.value) a function?
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 22836272
>>Is test(o.value) a function?
Not a function. It is a method. Given this:
if( /[^\d|]/.test(o.value) )

get rid of the "if" clause and you are left with:
/[^\d|]/.test(o.value)

So, you can clearly see that I am using a RegExp object. The if statement I wrote is equivalent to:

var t = new RegExp("[^\\d|]", "");
if( t.test( o.value) )

if you are NOT familiar with reular expressions I suggest you refer to:
http://www.webreference.com/js/column5/index.html

If you are already familiar with regular expressions, then skip to:
http://www.webreference.com/js/column5/methods.html

so you become familiar with the supported methods.

On another note, still not clear what the issue is. You said you wanted to allow pipes, which it does. But then you said you don't, in which case just remove the pipe symbol from my original regex and it will allow only digits
0
 

Author Comment

by:amukta
ID: 24716716
Thanks!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question