/    Sign up×
Community /Pin to ProfileBookmark

help needed> form validation script

Help will be appreciated! I want to a form validation script that will check a the following things of the user name value.
1. if no value is entered, client will get alert msg like ” Enter any user name”
2. If any charecter other than ”abcdefghijklmnopqrstuvwxyz0123456789-_” is enterted, client will get alert msg like “Use only characters and numbers in your user name!”
3. if length of the value is <2 and >20 is enter entered, client will get alert msg like ”Enter any user name from 2 to 20 charecters long”
4. if some certains words for example, Admin, Administrator, Operator, Manager are entered, client will get alert msg like ”You entered a restricted name. Use something else”.

I currently have this script:

function validate(field) {
var valid = “abcdefghijklmnopqrstuvwxyz0123456789-_”
var ok = “yes”;
var temp;
for (var i=0; i<field.value.length; i++) {
temp = “” + field.value.substring(i, i+1);
if (valid.indexOf(temp) == “-1”) ok = “no”;
}
if (ok == “no”) {
alert(“Use only characters and numbers in your user name!”);
field.focus();
field.select();
}
}

Can anyone help me making a script combining all those features?

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceApr 05.2006 — <i>
</i>var reserved = "|admin|administrator|operator|manager|";
//
function validate(field) {
if(!/^[a-z0-9-_]+$/i.test(field.value)) {
return validateFailed(field, "Use only characters and numbers in your user name!");
}
if(field.value.length &lt; 2
|| field.value.length &gt; 20) {
return validateFailed(field, "Your user name must be from 2 to 20 characters long!");
}
if(0 &lt;= reserved.indexOf("|"+field.value.toLowerCase()+"|")) {
return validateFailed(field, "That user name is reserved!");
}
return true;
}
function validateFailed(field, message) {
alert(message);
field.select();
field.focus();
return false;
}
Copy linkTweet thisAlerts:
@bhuiyanauthorApr 05.2006 — thanks for the script! if someone enters server_admin or site-admin or anything adding before or after those reserved names, script doesn't stop it. is it possible to have the script this way so that it will stop any name or word the contains any of those reserved names?
Copy linkTweet thisAlerts:
@phpnoviceApr 05.2006 — Sure, just change this:

var reserved = "|admin|administrator|operator|manager|";

to this:

var reserved = "[U]admin[/U]istrator operator manager";

and this:

if(0 <= reserved.indexOf("|"+field.value.toLowerCase()+"|")) {

to this:

if(0 <= reserved.indexOf(field.value.toLowerCase())) {
Copy linkTweet thisAlerts:
@bhuiyanauthorApr 05.2006 — thanks again. this is what i did:

var reserved = "administrator operator manger";

//

function validate(field) {

if(!/^[a-z0-9-_]+$/i.test(field.value)) {

return validateFailed(field, "Use only characters and numbers in your user name!");

}

if(field.value.length < 2

|| field.value.length > 20) {

return validateFailed(field, "Your user name must be from 2 to 20 characters long!");

}

if(0 <= reserved.indexOf(field.value.toLowerCase())) {

return validateFailed(field, "That user name is reserved!");

}

return true;

}

function validateFailed(field, message) {

alert(message);

field.select();

field.focus();

return false;

}

but if you use word ''siteadministrator'' or ''administrators'' or whatever with administrator word, it doesn't work.
Copy linkTweet thisAlerts:
@phpnoviceApr 05.2006 — Yep, I see that. Guess you'll have to use an array and loop though the reserved values for that check.
Copy linkTweet thisAlerts:
@bhuiyanauthorApr 05.2006 — i don't have much idea about it. i need your help.
Copy linkTweet thisAlerts:
@phpnoviceApr 05.2006 — Have you taken a [URL=http://www.webreference.com/programming/javascript/diaries/]JavaScript Tutorial[/URL]?
Copy linkTweet thisAlerts:
@bhuiyanauthorApr 05.2006 — sorry, i didn't. i just went though some of them. i am not much into this area. so everything seems very complicated to me.
Copy linkTweet thisAlerts:
@phpnoviceApr 05.2006 — Well, this site is intended to help you in your efforts to learn -- not just a place to get free contract-programming services. So, you can't expect everything to just get handed to you. You have to make the effort to learn, and to code, things for yourself.

Cheers.
Copy linkTweet thisAlerts:
@bhuiyanauthorApr 05.2006 — thanks a lot!
×

Success!

Help @bhuiyan 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.19,
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,
)...