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
372 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
 

Author Comment

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

Author Comment

by:amukta
ID: 22828921
I need it ASAP.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now