/    Sign up×
Community /Pin to ProfileBookmark

another quickie bout null value

This script works fine as long as var usename has some value. I’m trying to create an alert which will tell the viewer (after they call this function) that they need to enter a name if they leave the name field blank in the form. (The form is called fname.). I tried using if(usename==null) and that didn’t work either. Any ideas? Thanks.

<script language=”javascript”>

function printname()
{
var usename=document.fname.username.value;
if (usename==” “)
{
window.alert(“We needs a name”);
}
else
{
document.getElementById(‘namediv’).innerHTML=(usename+”, I’m very excited for you. Please answer all the questions honestly, because we know when you’re lying.”);
document.getElementById(‘namediv’).style.display=”block”;
}
}
</script>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@FangNov 29.2006 — function printname() {
if( /^w[w ]w$/i.test(document.fname.username.value) ) { // allows 'John Doe' but not ' John Doe '
alert('ok');
}
else {
alert('Enter a valid name');
}
}
Copy linkTweet thisAlerts:
@mrhooNov 29.2006 — This variation will accept names that contain common punctuation,

like 'Mrs. Mc'Entree' or 'Henri Brillit-Savern';

and remove leading and trailing spaces.

[CODE]function printname(){
var usename=document.fname.username.value;
usename=usename.match(/([a-z][a-z -.']+)/);
if(!usename){
document.fname.username.value= prompt('We need a name:');
printname();
}
else{
usename=usename[1];
usename=usename.replace(/ +/g,' ').replace(/ *$/g,'');
//handle usename;
}
}[/CODE]
×

Success!

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