/    Sign up×
Community /Pin to ProfileBookmark

Regular Expression help

I have the following function that i am using to check fields on a form for invalid characters (mostly foreign language characters). It works if the input string is all invalid characters (i.e. 日本&#35486?, and if there are VALID characters before the invalid ones (i.e. ABC日本&#35486?, but not if there are valid characters before AND after the invalid ones. Can anyone tell me what i’m doing wrong? Thanks!

function chkchar2()
{

for (var j=0;j < document.forms[0].length;j++)
{
if (document.forms[0].elements[j].type == “text” || document.forms[0].elements[j].type == “textarea”)
{
for (var i = 0; i < document.forms[0].elements[j].value.length; i++)
{
var myregexp = new RegExp(“[A-Z]|[a-z]|\s|[_]|[0-9]|[.]|[‘]|[“]|[!]|[@]|[#]|[$]|[%]|[&]|[*]|[(]|[)]|[-]|[=]|[+]|[~]|[;]|[:]|[<]|[>]|[.]|[?]|[/]|[|]|[\]|[{]|[}]|[\^]”);

for (var x = 0; x < document.forms[0].elements[j].value.length; x++)
{
var match = myregexp.exec(document.forms[0].elements[j].value.substring(x));

if (match != null) {

} else {alert(“invalid”);
j++;

}

}
}
}
}

}

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KrylesNov 14.2007 — You can throw numbers/letters/LETTERS together like /w or [A-Za-z0-9]

http://tools.netshiftmedia.com/regexlibrary/#

I like this place to fool around and go step by step through my reg expressions
Copy linkTweet thisAlerts:
@ZeroKilledNov 15.2007 — i would use unicode that range your required language. but first you should do a research about the range for latin character within the unicode charset including symbol. the pattern will be so short as:

/[^u0000-u001ff]/

that is just a sample of possible pattern. or you can do the long way, type every valid value on the pattern:

/[^ws`~!@#$%^&*()+{}|":<>?-=[];',./]/

i had not tested the pattern but basically the pattern match any character that isn't contained there. this pattern contain character as: latin character, a to z in both case, base tenth number (0 to 9), any white space character (including carriage return), and every english keyboard symbol. important: notice that most symbol had to be escaped because they have special meaning in regular expression, so is a little hard to read the regex.
×

Success!

Help @reddeath_ spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...