/    Sign up×
Community /Pin to ProfileBookmark

js Regex problem

Hello guys

i need validate this format ###-####### (where # is a number of course)
like
123-1234567
001-5469898
when the user fills the field

the point is i have this and doesnt work yet

[CODE]
function numerofacturacontrol(e,str){
var key = window.event ? e.keyCode : e.which;

if((key==null) //null
|| (key==0) //null
|| (key==8) //backspace
|| (key==9) //horizontal tab
//|| (key==13) //enter
|| (key==27) //scape
) return true;

var myregex = new [b]RegExp(“^(\d{3,3})+(-{1,1})?(\d{7,7})?$[/b]”,”g”);
return myregex.test(str + String.fromCharCode(key));

}
[/CODE]

how i can resolve this?

thanks for advanced

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@DokSep 27.2007 — [CODE]var s = "012-3456789";
var reg = /^d{3}-d{7}$/;
if (s.match(r))
// True if s matches r
else
// Otherwise false[/CODE]
Copy linkTweet thisAlerts:
@dr_pompeiiauthorSep 27.2007 — Hi Dok

thanks for the reply

it doesnt work yet ?

[CODE]
function numerofacturacontrol(e,str){
var key = window.event ? e.keyCode : e.which;

if((key==null) //null
|| (key==0) //null
|| (key==8) //backspace
|| (key==9) //horizontal tab
//|| (key==13) //enter
|| (key==27) //scape
) return true;
[b]var myregex = new RegExp("^\d{3}-\d{7}$","g"); [/b]
return myregex.test(str + String.fromCharCode(key));
}
[/CODE]

even, i cant write nothing in the textfield

remember that the user must fill data in this format ###-####### for each character

maybe your code work, coz you gave all the number
[CODE]var s = "012-3456789";[/CODE]

regards
Copy linkTweet thisAlerts:
@Orc_ScorcherSep 27.2007 — You're calling this function on a keypress event and want to allow inputs that are not yet complete like "123-", right? Then try myregex = /^(d|$){3}(-|$)(d|$){7}/
Copy linkTweet thisAlerts:
@dr_pompeiiauthorSep 28.2007 — thanks for the reply

it works!!!

but i have a correction
[CODE]
var myregex = new RegExp("^(\d|$){3}(-|$)(\d|$){7}$","g");
[/CODE]

check it that is <--- twice

regards

god bless you ?
Copy linkTweet thisAlerts:
@Orc_ScorcherSep 28.2007 — In strings you have to escape the backslash, in regexp literals you must not. But I missed the final $, that correction is correct. The "g" flag is pointless here and using new RegExp just creates a new object on each keypress which must be garbage collected. It's not terribly important because even the best typist won't manage more than 10 keystrokes per second, but if the better solution is even shorter and easier to type then you might as well use it.
×

Success!

Help @dr_pompeii 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.15,
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,
)...