469,890 Members | 1,432 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

to verify a string (only some charachters remain)

den
If in one input text the characters inserted aren't one of this:
a-z A-Z 0-9 _ -

I would like delete them.

so if one write this:
a00?@ e6>>
the result will have to be so:
a00e6
Mar 8 '07 #1
10 1456
den wrote:
If in one input text the characters inserted aren't one
of this: a-z A-Z 0-9 _ -

I would like delete them.

so if one write this:
a00?@ e6>>
the result will have to be so:
a00e6
<html>

<head>
<script type="text/javascript">
function getkey(e) {
if (window.event)
return window.event.keyCode;
else if (e)
return e.which;
else
return null;
}

function goodchars(e, goods) {
var key, keychar;
key = getkey(e);
if (key == null) return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();
goods = goods.toLowerCase();
if (goods.indexOf(keychar) != -1)
return true;
if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
return true;
return false;
}

</script>
</head>

<body>
<input type="text" name="myname"
onKeyPress="return goodchars(event,
' 0123456789azertyuiopqsdfghjklmwxcvbnAZERTYUIOPQSDF GHJKLMWXCVBN_-'
)">
</body>

</html>

Hope this helps,

--
Bart

Mar 8 '07 #2
Ivo
"den" writes
If in one input text the characters inserted aren't one of this:
a-z A-Z 0-9 _ -
I would like delete them.
so if one write this: a00?@ e6>>
the result will have to be so: a00e6
This sounds like a typical job for regular expressions. Try this:

newstring = oldstring.replace( /[^\w_-]/g, '' );

\w matches letters and digits, _ and - match themselves, and the square
brackets with ^ turn the rules around to match anything but the mentioned
characters. The results are replaced with the second parameter, the empty
string.
hth
Ivo
http://4umi.com/web/javascript/regex.htm
Mar 8 '07 #3
den
Il Thu, 8 Mar 2007 17:59:56 +0100, Ivo ha scritto:
"den" writes
>If in one input text the characters inserted aren't one of this:
a-z A-Z 0-9 _ -
I would like delete them.
so if one write this: a00?@ e6>>
the result will have to be so: a00e6

This sounds like a typical job for regular expressions. Try this:

newstring = oldstring.replace( /[^\w_-]/g, '' );

\w matches letters and digits, _ and - match themselves, and the square
brackets with ^ turn the rules around to match anything but the mentioned
characters. The results are replaced with the second parameter, the empty
string.
ok thanks;
If I want implemeted your solution with also this:
to accept also space but only inner the word
and only if not are consecutive;
example
if one insert this (consider * as the space) :
*AA***BB**CC***
the result will be:
AA*BB*CC
so I can have the spaces only inner the string and not consecutives;

if is difficult to change your solution, then you can resolve so?:
1st step: remove the * in excess;
2nd how your solution that accept also space;



Mar 8 '07 #4
Ivo
"den" writes
Ivo ha scritto:
>"den" writes
>>If in one input text the characters inserted aren't one of this:
a-z A-Z 0-9 _ -
I would like delete them.
so if one write this: a00?@ e6>>
the result will have to be so: a00e6

This sounds like a typical job for regular expressions. Try this:

newstring = oldstring.replace( /[^\w_-]/g, '' );

\w matches letters and digits, _ and - match themselves, and the square
brackets with ^ turn the rules around to match anything but the mentioned
characters. The results are replaced with the second parameter, the empty
string.

ok thanks;
If I want implemeted your solution with also this:
to accept also space but only inner the word
and only if not are consecutive;
example
if one insert this (consider * as the space) :
*AA***BB**CC***
the result will be:
AA*BB*CC
Here:
newstring = oldstring.replace( /[^\w\s_-]/g, '' ).replace( /^\s+|\s+$/g,
'' ).replace( /\s+/g, ' ' );

The first expression is almost like the above, except for the \s in there to
allow spaces. In the second call to replace, the ^ and $ are anchors,
specifying the match must occur at the start or the end respectively, \s
matches all sorts of whitespace characters like normal space, tab and
formfeed. The + makes it match a whole run of matching characters. In the
third and last call to the method, the remaining spaces are replaced with a
single space. Presto!
hth
Ivo
http://4umi.com/web/javascript/regex.htm
Mar 8 '07 #5
On Mar 8, 1:09 pm, "Ivo" <n...@thank.youwrote:
If I want implemeted your solution with also this:
to accept also space but only inner the word
and only if not are consecutive;
Here:
newstring = oldstring.replace( /[^\w\s_-]/g, '' ).replace( /^\s+|\s+$/g,
'' ).replace( /\s+/g, ' ' );
Or this (not tested)-
newstring=oldstring.replace( /^(\s+)?|[^\w\s-]|(\s+)?$/g,'')
Mar 8 '07 #6
On Mar 9, 2:46 am, scripts.cont...@gmail.com wrote:
On Mar 8, 1:09 pm, "Ivo" <n...@thank.youwrote:
If I want implemeted your solution with also this:
to accept also space but only inner the word
and only if not are consecutive;
Here:
newstring = oldstring.replace( /[^\w\s_-]/g, '' ).replace( /^\s+|\s+$/g,
'' ).replace( /\s+/g, ' ' );

Or this (not tested)-
newstring=oldstring.replace( /^(\s+)?|[^\w\s-]|(\s+)?$/g,'')
But the second pattern doesn't replace metacharacters in the very
beginning of string (with no whitespaces before). At least, that's the
rough result of mine.
Try test it with '/$%# ab cd_ 30-asd' or whatever.

Mar 9 '07 #7
On Mar 8, 8:39 pm, "Cah Sableng" <cahsabl...@gmail.comwrote:
But the second pattern doesn't replace metacharacters in the very
beginning of string (with no whitespaces before). At least, that's the
rough result of mine.
Try test it with '/$%# ab cd_ 30-asd' or whatever.

/(^\s+)?[^\w\s-]*(\s+$)?/

problems-
it will alow space if there is a character before space e.g. this-
~ Something
will give- " Something"

spaces will be allowed between characters-
xxx xxxx
will give you- same as above

Mar 9 '07 #8
On Mar 9, 10:38 am, scripts.cont...@gmail.com wrote:
On Mar 8, 8:39 pm, "Cah Sableng" <cahsabl...@gmail.comwrote:
But the second pattern doesn't replace metacharacters in the very
beginning of string (with no whitespaces before). At least, that's the
rough result of mine.
Try test it with '/$%# ab cd_ 30-asd' or whatever.

/(^\s+)?[^\w\s-]*(\s+$)?/

problems-
it will alow space if there is a character before space e.g. this-
~ Something
will give- " Something"

spaces will be allowed between characters-
xxx xxxx
will give you- same as above
/(^[\s\W]+)?[^\w\s-]*([\s\W]+$)?/

but it removes the hyphen in the beginning of string with or without
whitespace(s) before.
failed test string : ' - $#as34 23 - '

Mar 9 '07 #9
On Mar 9, 1:06 am, "Cah Sableng" <cahsabl...@gmail.comwrote:
/(^[\s\W]+)?[^\w\s-]*([\s\W]+$)?/

but it removes the hyphen in the beginning of string with or without
whitespace(s) before.
nice. this one will not remove hyphens :
/(^[^\w\-]+)?[^\w\s-]*([^\w\-]+$)?/g

Mar 9 '07 #10
On Mar 9, 2:29 am, scripts.cont...@gmail.com wrote:
On Mar 9, 1:06 am, "Cah Sableng" <cahsabl...@gmail.comwrote:
/(^[\s\W]+)?[^\w\s-]*([\s\W]+$)?/
nice. this one will not remove hyphens :
/(^[^\w\-]+)?[^\w\s-]*([^\w\-]+$)?/g

and this one will also remove the extra spaces from between words -
..replace(/(^[^\w\-]+)?((\s)|[^\w\s])*([^\w\-]+$)?/g,'$3')

Mar 10 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jamie Wright | last post: by
11 posts views Thread by James Stroud | last post: by
2 posts views Thread by Vilmar Brazão de Oliveira | last post: by
4 posts views Thread by Lauren Quantrell | last post: by
10 posts views Thread by David Graham | last post: by
1 post views Thread by 2D Rick | last post: by
30 posts views Thread by diane | last post: by
3 posts views Thread by Nightcrawler | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.