473,378 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 1565
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jamie Wright | last post by:
I have been using PHP for a while now, although my knowledge of the inbuilt functions is pretty lacking. I am trying to remove a 'space' from the middle of a string. I reckon I could split the...
11
by: James Stroud | last post by:
Hello, I am looking for a nice way to take only those charachters from a string that are in another string and make a new string: >>> astr = "Bob Carol Ted Alice" >>> letters = "adB" >>>...
2
by: Miguel Orrego | last post by:
Hi, I have a page that pulls data from a database, one of the fields is Notetext which frequently contains an email. I then want to pass this onto another page, that updates the email field in...
2
by: Vilmar Brazão de Oliveira | last post by:
HI, How verify MIME type of file?? Which components should I use? Still now I couldn't find anything! OBS.: I don´t to check file extensions! Thanks,
4
by: Lauren Quantrell | last post by:
Is there anyway to extract part of a string in a stored procedure using a parameter as the starting point? For example, my string might read: x234y01zx567y07zx541y04z My Parameter is an nvarchar...
10
by: David Graham | last post by:
Hi I have been busy going through the last weeks postings in an attempt to absorb javascript syntax (I guess it's not possible to just absorb this stuff in a passive way - I'm getting way out of...
1
by: 2D Rick | last post by:
I open a InputBox for specific formatted data and pass return to a variable. The format and length remain constant: yyCC looks like 05MR Is there a quick way to verify the input meets the format...
30
by: diane | last post by:
I've got an application running with table-based security: i capture the user's windows login with fOsusername, then have them enter a password checked against their username/login in my own table....
3
by: Nightcrawler | last post by:
I have a website that does the following: 1. it accepts a keyword through a textbox in the UI 2. once the submit button is clicked it goes out and spiders a few websites using the keyword...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.