/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Need Appropriate Reg Exp & Drop Down Check

I’m having a couple of issues with my form.

[B]1.[/B] I need a regular expression that allows only alpha chars for a name field. My regular expression is allowing this (John D##). Also my expression [B]isn’t[/B] allowing spaces between first and last names like this (John Doe).

[CODE]
function checkIllegalChars(theuser_name, error){

var name_exp = /[a-zA-Z]+/;

for(var i=0; i < theuser_name.length; i++)
{
the_char = theuser_name.charAt(i);

if (!name_exp.test(the_char))
{
error.val=2;
return false;
}

}
return true;
}
[/CODE]

[B]2.[/B] My form is suppose to make sure that a city is selected from the drop down menu. Here’s my code for that:

[CODE]
correct=checkSelOpts(sel_opt, error);

if (!correct)
{
error_mes += errors[error.val] + ‘<br>’;
}
…//[COLOR=”Blue”]else statement is further down…it’s not the problem[/COLOR]


function checkSelOpts(city_opts, error){

if (city_opts=”none”)
{
error.val=4;
return false;
}

return true;

}
[/CODE]

Now I’ve checked Firebug and “city_opts” has the value of whatever is selected. So that part is working. But when I hit submit, the form isn’t recognizing that a city was indeed selected. It still shows an error message regardless of a city being selected or not. Please help.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@scragarOct 14.2008 — I'm having a couple of issues with my form.

[B]1.[/B] I need a regular expression that allows only alpha chars for a name field. My regular expression is allowing this (John D##). Also my expression [B]isn't[/B] allowing spaces between first and last names like this (John Doe).

[CODE]
function checkIllegalChars(theuser_name, error){

var name_exp = /[a-zA-Z]+/;

for(var i=0; i < theuser_name.length; i++)
{
the_char = theuser_name.charAt(i);

if (!name_exp.test(the_char))
{
error.val=2;
return false;
}

}
return true;

}
[/CODE]

[/quote]

<i>
</i>function checkIllegalChars(theuser_name, error){
if(/^w+( w+)*$/.test(theuser_name)){
error.val=2;
return false;
}else{
return true;
}
}



[B]2.[/B] My form is suppose to make sure that a city is selected from the drop down menu. Here's my code for that:

[CODE]
correct=checkSelOpts(sel_opt, error);

if (!correct)
{
error_mes += errors[error.val] + '<br>';
}
...//[COLOR="Blue"]else statement is further down...it's not the problem[/COLOR]

...
function checkSelOpts(city_opts, error){

if (city_opts="none")
{
error.val=4;
return false;
}

return true;

}
[/CODE]


Now I've checked Firebug and "city_opts" has the value of whatever is selected. So that part is working. But when I hit submit, the form isn't recognizing that a city was indeed selected. It still shows an error message regardless of a city being selected or not. Please help.[/QUOTE]

comparisons use == or === not =
<i>
</i> if (city_opts=[b]=[/b]"none")
Copy linkTweet thisAlerts:
@banksworld1986authorOct 15.2008 — <i>
</i>function checkIllegalChars(theuser_name, error){
if(/^w+( w+)*$/.test(theuser_name)){
error.val=2;
return false;
}else{
return true;
}
}



comparisons use == or === not =
<i>
</i> if (city_opts=[b]=[/b]"none")
[/QUOTE]


That's it...Thanks scragar. It worked. ?
×

Success!

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