/    Sign up×
Community /Pin to ProfileBookmark

help: validation of input type text for telephone number

Hi people!
I’m sorry for my bad english, please answer me in simple words.

I’m trying to validate an input type text html tag with JS.

There is an array (prefisso[]) with a lot of dial codes.
The first 3 characters of the value to check must be one of the elements of “prefisso[]”.
The value to check must be longer than 9 chars (min 10).

Were I’m wrong?

thank you!

[CODE]
function verifytel(elem) {
var prefisso=new Array();

prefisso[0]=”331″;
// …
// FOLLOWS A LOT OF DIAL CODES
// …
prefisso[36]=”377″;
prefisso[37]=”331″;
prefisso[38]=”377″;

var check=false;
if(!isNaN(elem.value)) {
if((elem.value.length>=10)) {
for(var i=0; i<prefisso.length; i++) {
if(elem.value.startsWith(prefisso[i])==true) {
check=true;
elem.style.backgroundColor=”#00ff00″;
}
}
if(check==false) alert(“Invalid telephone number.”);
}
}
else alert(“Please insert only numbers, no dots, blank spaces, etc.”);
}[/CODE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@jalarieSep 06.2011 — Try changing "startsWith" to "substring(0,3)" to see if that works.
Copy linkTweet thisAlerts:
@leonixyzauthorSep 06.2011 — thank you! now works!
×

Success!

Help @leonixyz 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.17,
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,
)...