/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Expected ; but where?

I have some javascript that works perfectly with no errors but when i tried adding a new function i got this error message

[QUOTE]

Error: Expected ;

[/QUOTE]

The function that is causing the problem is this

[CODE]function checkQuickSearch()
{
//var jobs = document.QuickSearch.job.options;
//var chosenjob = jobs[jobs.selectedIndex].value;

// var loc = document.QuickSearch.loc.options;
//var chosenloc = loc[loc.selectedIndex].value;

if(chosenjob==”default”)
{
alert(“Please select a job type”);
return false;
}else if(chosenloc=”default”)
{
alert(“Please select a location”);
return false;
}else
{
alert(“Well done…..validation passed”);
return true;
}
}[/CODE]

I won’t include the whole JS but just the functions that are getting called by the handlers

[CODE]window.onload = handlers;

——————————————————————–

// Function to register event handlers
function handlers()
{
document.addressForm.onsubmit = checkEmail;
document.QuickSearch.send.onsubmit checkQuickSearch;
attachFormHandlers()
}

——————————————————————

function checkEmail()
{
// Regular Expression for Phone Number
var regex=/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+.)+[a-zA-Z0-9.-]{2,4}$/;

// Test input
if(regex.test(document.addressForm.email.value))
{
// If good then allow form to submit
return true;
}else{
//If bad then warn user and block submission
alert(“Please enter a valid email address.”);
return false;
}
}

function checkQuickSearch()
{
//var jobs = document.QuickSearch.job.options;
//var chosenjob = jobs[jobs.selectedIndex].value;

// var loc = document.QuickSearch.loc.options;
//var chosenloc = loc[loc.selectedIndex].value;

if(chosenjob==”default”)
{
alert(“Please select a job type”);
return false;
}else if(chosenloc=”default”)
{
alert(“Please select a location”);
return false;
}else
{
alert(“Well done…..validation passed”);
return true;
}
}

//——————————————————————

function attachFormHandlers()
{

var form = document.getElementById(‘form1’) // get the form

if (document.getElementsByTagName)//make sure were on a newer browser
{
var objInput = document.getElementsByTagName(‘input’); // store all input fields
for (var iCounter=0; iCounter<objInput.length; iCounter++)

objInput[iCounter].onchange = function(){return attach(this);} //attach the onchange to each input field
}

form.onsubmit = function(){return validate();} //attach validate() to the form
}[/CODE]

What i want to do is get the value from the select box and if it says default, then display an error message…i thought it would be simple ?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@Angry_Black_ManMar 01.2008 — admittedly, i didnt go through the whole code, but the first thing i saw was that where you intend to do a comparison, you instead set the variable:

}else if([COLOR="Red"]chosenloc="default"[/COLOR])
{
alert("Please select a location");
Copy linkTweet thisAlerts:
@ZeroKilledMar 01.2008 — most probably the syntax error is in this line [B]document.QuickSearch.send.onsubmit checkQuickSearch;[/B] on the function handler. notice that you got two statement on the same line and they're not separated with the semicolon. guess you forgot the assigment operator (=).

also noticed what [B]aaron[/B] point out, but i guess that wouldn't throw an error.
Copy linkTweet thisAlerts:
@AdRock952authorMar 01.2008 — Thanks guys for the advice...

I have nearly finished what i have to do and am learning more and more all the time.

I have got a different error now an d i know why it is becuase the rest of the javascript works.

On a certain page, i don't have a form called form1 and one of the functions is trying to get it by id and becuase it doesn't exist it's reporting a [B]'null' is null or not an object[/B]

How do i set this variable only if the forme exists on the page?

[CODE]var form = document.getElementById('form1') // get the form[/CODE]
Copy linkTweet thisAlerts:
@ZeroKilledMar 01.2008 — a quick patch could be to exit the function if the form doesn't exist:
<i>
</i>var form = document.getElementById('form1') // get the form
if(!form)return;
Copy linkTweet thisAlerts:
@AdRock952authorMar 01.2008 — Heoprfully this should be the last ime i bother anyone with this as i am so close

I don't get any errors anymore and all other validation passes but i can't get the [B]function attachSearchHandlers()[/B] to call the other functions to be checked

All it is supposed to do is check if the keywords are valid and if they have entered a valid amount in the salary field

[CODE]window.onload = handlers;

//--------------------------------------------------------------------

// Function to register event handlers
function handlers()
{
document.addressForm.onsubmit = checkEmail;
attachSearchHandlers();
attachFormHandlers();
}

//------------------------------------------------------------------

function checkEmail()
{
// Regular Expression for Phone Number
var regex=/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+.)+[a-zA-Z0-9.-]{2,4}$/;

// Test input
if(regex.test(document.addressForm.email.value))
{
// If good then allow form to submit
return true;
}else{
//If bad then warn user and block submission
alert("Please enter a valid email address.");
return false;
}
}

function attachSearchHandlers()
{
var searchform = document.getElementById('fullsearch'); // get the form}

if(searchform)
{
return search();
}else{
return false;
}
}

function Search()
{
if(checkMoney() && checkKeywords()) alert("Validation passed here too");
}

function checkMoney()
{
var regex = /^d+(?:.d{0,2})?$/;

if(regex.test(document.FullSearch.salary.value))
{
// If good then allow form to submit
return true;
}else{
//If bad then warn user and block submission
alert("Please enter a valid amount.");
return false;
}
}

function checkKeywords()
{
var regex=/^([a-zA-Z0-9 ]{4,50})$/;
var key = document.FullSearch.keywords.value;

// do the comparison, if we have a match write thank you or else the email is invalid
if(key !="")
{
if (regex.test(key))
{
return true;
}
else
{
//If bad then warn user and block submission
alert("Please enter some valid keywords.");
return false;
}
}else{ return true; }
}[/CODE]
Copy linkTweet thisAlerts:
@ZeroKilledMar 01.2008 — when the [B]search[/B] function should be fired? within the load event or on submit form? of course, the handler function is fired on load, then it execute attachSearchHandler which doesn't attach a handler to any listener. that is, it is firing the handler that should be used when user submit form. maybe this should help:

<i>
</i>function attachSearchHandlers()
{
var searchform = document.getElementById('fullsearch'); // get the form}

<i> </i>if(searchform) searchform.onsubmit = Search;
<i> </i>else{
<i> </i> return false;
<i> </i>}
}
×

Success!

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