/    Sign up×
Community /Pin to ProfileBookmark

Streamline Script Tips

Hey all.

I’ve blinded myself for today perfecting my forms for a site. The following script works fine but I’d like to know if there are any redundancies. The variables are defined and used often elsewhere.

I’m just calculated out so … hoping for some streamlining advice.

[CODE]var posting = ‘false’;
var input;
var estatus;
var iframe = ‘false’;
var process;
var valid;
var sent;

function RunForm(action) {

if (action == ‘post’) {
if (posting != ‘true’) {
if (/^([_a-z0-9-]+)(.[_a-z0-9-]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*(.[a-z]{2,4})$/.test(input.value)) {

setTimeout(“posting = ‘true’;”, 0);
estatus.style.display = ‘block’;
setTimeout(“content.focus();”, 0);
if (iframe != ‘true’) {process.innerHTML = ‘<iframe name=”process” frameborder=”0″ scrolling=”no”></iframe>’; iframe = ‘true’;}
return true;

} else {

valid = ‘false’;
alert(‘Please enter a valid email address.’);
input.focus();
input.value = ”;
valid = ‘true’;
return false;

}
}

else return false;

} else {

input.value = ‘Confirmation Email Sent’;
sent = ‘true’;
estatus.style.display = ”;
posting = ‘false’;

}
}[/CODE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@DseldentreimanApr 05.2008 — To me there don't appear to be any redundancies.

If you want a bit of extra speed, I would recommend changing your final version so that the variables have 1 character and eliminating whitespace. Good Coding?

~David
Copy linkTweet thisAlerts:
@Declan1991Apr 05.2008 — [code=html]
<script type="text/javascript">
var posting = false;
var input;
var estatus;
var iframe = false;
var process;
var valid;
var sent;

function RunForm(action) {

if (action == 'post') {
if (!posting) {
if (/^(w+)(.w+)*@(w+)(.w+)*(.[a-z]{2,4})$/.test(input.value)) {
posting = true;
estatus.style.display = 'block';
content.focus();
if (!iframe) {process.innerHTML = '<iframe name="process" frameborder="0" scrolling="no"></iframe>'; iframe = true;}
return true;

} else {

valid = false;
alert('Please enter a valid email address.');
input.focus();
input.value = '';
valid = true;
return false;

}
}

else return false;

} else {

input.value = 'Confirmation Email Sent';
sent = true;
estatus.style.display = '';
posting = false;

}
}
</script>
[/code]

Difference are basically using booleans for true and false instead of strings, and removing the setTimeout("whatever();",0); and replacing [_a-zA-Z0-9-] with w. I also replaced [a-zA-Z0-9-] with that, but you can change that back if you want.
×

Success!

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