Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-08-2003, 09:01 PM   PM User | #1
oldflint
New to the CF scene

 
Join Date: Apr 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
oldflint is an unknown quantity at this point
Help!!! RegExp for any printable characters

Apparently got stuck with the simple task to validate an entry with any PRINTABLE characters (ASCII between 32 and 126) using search (or any other) method. Something like:

if (str.search( . . . ) != -1)
return true;
else
return false;

I was thinking more like using Predefined Sets, i.e. [print:]
or [:graph:]. but nobody really gives any examples of syntax with those things.

Any idea would be greatly appreciated!!!
oldflint is offline   Reply With Quote
Old 04-08-2003, 10:47 PM   PM User | #2
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
How about the whole string method for ya?
Code:
String.prototype.isPrintable = function()
{
    var pat = '';
    for ( var i = 32; i <= 126; i++ )
    {
        pat += "\\x" + i.toString(16);
    }
    var regex = new RegExp( "[^" + pat + "]" );
    return ( !regex.test( this ) )
}
__________________
My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
“Minds are like parachutes. They don't work unless they are open”
“Maturity is simply knowing when to not be immature”
beetle is offline   Reply With Quote
Old 04-08-2003, 11:00 PM   PM User | #3
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Code:
String.prototype.isPrintable=function(){
    var re=/^[\x20-\x7e]*$/;
    return re.test(this);
}
Would be much more efficient than what Beetle wrote.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

Last edited by liorean; 04-08-2003 at 11:05 PM..
liorean is offline   Reply With Quote
Old 04-08-2003, 11:01 PM   PM User | #4
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
Ooh, ranges with ascii chars. Missed that one.

Nice liorean

Is it better to test the entire string for compliancy, or let the regex look at the compliancy of each char? (if you follow me, using a negated character set)
Code:
String.prototype.isPrintable=function(){
    var re=/[^\x20-\x7e]$/;
    return !re.test(this);
}
__________________
My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
“Minds are like parachutes. They don't work unless they are open”
“Maturity is simply knowing when to not be immature”

Last edited by beetle; 04-08-2003 at 11:05 PM..
beetle is offline   Reply With Quote
Old 04-08-2003, 11:11 PM   PM User | #5
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Well, actually I realised that your code would work in any case, since you would have returned true only if no character without the range existed in it. I just used my start and end from my habit of making sure the whole string is checked. This would actually be more efficient:
Code:
String.prototype.isPrintable=function(){
    var re=/[^ -~]/;
    return !re.test(this);
}
(Your last code wouldn't - you check for a negated charset followed by end of string.)
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 04-08-2003, 11:12 PM   PM User | #6
oldflint
New to the CF scene

 
Join Date: Apr 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
oldflint is an unknown quantity at this point
Wow!

Thank you gentlemen big time!

But say, I gotta take care about a different locale. Not only Latin alphabet. does anybody know how to use those predefined sets, i.e. [:print:] or [:graph:], etc.?

THANKS AGAIN!!!
oldflint is offline   Reply With Quote
Old 04-08-2003, 11:13 PM   PM User | #7
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
JavaScript doesn't handle the predefined sets, just a subset of PCRE. You'll have to specify the ranges for the characters in question.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 04-08-2003, 11:13 PM   PM User | #8
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
Quote:
Originally posted by liorean
(Your last code wouldn't - you check for a negated charset followed by end of string.)
Oops, that was a typo.
__________________
My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
“Minds are like parachutes. They don't work unless they are open”
“Maturity is simply knowing when to not be immature”
beetle is offline   Reply With Quote
Old 04-08-2003, 11:32 PM   PM User | #9
oldflint
New to the CF scene

 
Join Date: Apr 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
oldflint is an unknown quantity at this point
Thank you again, gentlemen!
oldflint is offline   Reply With Quote
Old 04-09-2003, 02:17 PM   PM User | #10
Choopernickel
Regular Coder

 
Join Date: Apr 2003
Location: Atlanta, GA
Posts: 487
Thanks: 0
Thanked 0 Times in 0 Posts
Choopernickel is an unknown quantity at this point
You have no chance make your time!


(sorry... I'll get my coat on the way out...)
Choopernickel is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

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

Forum Jump


All times are GMT +1. The time now is 06:21 AM.


Advertisement
Log in to turn off these ads.