/    Sign up×
Community /Pin to ProfileBookmark

Passing object to a function

Hi,

I wrote a function that that is supposed to check a form. But it doesn’t. Where is the error ?

function checkJobApplication(application) {
if (application.position.value = “”) ) {
alert(“Please enter the desired position !”);
application.position.focus();
return false;
} else if

}

The function is called like this:

<form action=”career.php” method=”post” onsubmit=”return checkJobApplication(this.form)”>

to post a comment
JavaScript

18 Comments(s)

Copy linkTweet thisAlerts:
@7studOct 27.2004 — Is there actually an element in the form named 'position'?

If there is try this:

[color="maroon"]

onsubmit="return checkJobApplication([color="red"]this[/color])

[/color]
Copy linkTweet thisAlerts:
@7studOct 27.2004 — dup
Copy linkTweet thisAlerts:
@compuboy1010authorOct 27.2004 — Is there actually an element in the form named 'position'?

If there is try this:

[color="maroon"]document.application.position.value = ""[/color] [/QUOTE]


Yes, there is. >>I tried document.application.position.value = ""<< before. That doesn't work either.
Copy linkTweet thisAlerts:
@7studOct 27.2004 — Sorry, I was editing my post...you don't need 'document' if you already have a reference to the form. I think the problem is with the parameter you are passing.
Copy linkTweet thisAlerts:
@KorOct 27.2004 — if (application.position.value=[color=red]=[/color]"")

there must be a bitwise comparation not an assignment
Copy linkTweet thisAlerts:
@compuboy1010authorOct 27.2004 — function checkJobApplication(application) {

if (application.position.value == "") ) {

alert("Please enter the desired position !");

application.position.focus();

return false;

} else if (application.name_l.value == "") {

...

}

<form action="career.php" method="post" onsubmit="return checkJobApplication(this)">

<table>

<tr>

<td>Position:<span class="required">*</span></td>

<td><input type="text" name="position" size="35" /></td>





That doesn't work either.
Copy linkTweet thisAlerts:
@7studOct 27.2004 — You have an error here:

if (application.position.value = "")[color="red"] ) [/color]{
Copy linkTweet thisAlerts:
@7studOct 27.2004 — [color="maroon"]if (application.position.value="")[/color]

A good way to avoid that '=' versus '==' mistake is to do this:

[color="maroon"]if("" = application.position.value)[/color]

Then, you will get an error on that line instead of the script continuing on its merry way. On one of my very first programs, I spent about 8 hours tracking down an '=' versus '==' mistake.
Copy linkTweet thisAlerts:
@CharlesOct 27.2004 — Better yet, use [font=monospace]if (/S/.test (application.position.value))[/font] to test for the existance of at least one non-white space character.
Copy linkTweet thisAlerts:
@compuboy1010authorOct 27.2004 — Then, you will get an error on that line instead of the script continuing on its merry way. On one of my very first programs, I spent about 8 hours tracking down an '=' versus '==' mistake.[/QUOTE]

Thank you. Now it works for some reason.

The next question I have is: Would it be better to have a function for every field and then call a function application(application) from the form, so that the function application calls all the required functions for the application form? Like this?

function application(application) {

checkName(application);

checkPosition(application);

}

Or have one function test all fields. I am a beginner.

I was just wondering because I also have to test the postalcode in other forms. What would be the best way?
Copy linkTweet thisAlerts:
@compuboy1010authorOct 27.2004 — Better yet, use [font=monospace]if (/S/.test (application.position.value))[/font] to test for the existance of at least one non-white space character.[/QUOTE]

??? Where did you get that from. I don't understand that at all. Is there any documentation on this ?
Copy linkTweet thisAlerts:
@7studOct 27.2004 — Thank you. Now it works for some reason.[/quote]

Maybe it had to do with those three errors you corrected? ?

Would it be better to have a function for every field and then call a function application(application) from the form, so that the function application calls all the required functions for the application form? Like this?

function application(application) {

checkName(application);

checkPosition(application);

}[/quote]


I don't see much point in the 'wrapper' function. Why would you want to do that? Presumably, since you are going to have to validate each field differently, you are going to have to perform different operations, so that would mean separate functions. If you find yourself writing the same code over and over again for your separate functions, then you should consider combining them into one function and passing an appropriate parameter(s) to the function.


if (/S/.test (application.position.value))
Anything enclosed in '/' and '/' is called a 'regular expression'. If you look inside those symbols there is 'S' which represents 'any non white space character'. test() is a method of a regular expression object, and it searches the string argument you supply for a match to the regular expression, and then returns true or false. Regular expressions are not a javascript construct, and they exist in many languages.

See here for regex info:

beginning tutorial: http://www.sitepoint.com/article/expressions-javascript

list of symbols: http://www.siteexperts.com/tips/functions/ts23/page1.asp

a library of patterns: http://www.regexlib.com/DisplayPatterns.aspx
Copy linkTweet thisAlerts:
@compuboy1010authorOct 27.2004 — Thank you very much for the thorough explanation.

Actually, I use forms quite a lot and would like to use the wrapper functions. I just tested it:

function applicationCheckP1(form) {

checkPosition(form);

checkName(form);

checkAddress(form);

checkEmail(form);

checkPhoneNumber(form);

}


function checkPosition(form) {

if (!form.position.value) {

alert("Please enter desired position !");

form.position.focus();

return false;

}

return true;

}

<form action="career.php" method="post" onsubmit="return applicationCheckP1(this)">

etc ...

If I don't fill out anything, the alert messages come up one after the other. But it doesn't allow me enter anything. After the last message the form is sent anyway. Why ?
Copy linkTweet thisAlerts:
@KorOct 27.2004 — 
if (!form.position.value) {
[/quote]


Not a wise condition. In fact anytime you have a value for your inputs, and that is a '' string...

You must use:

if (form.position.value=='') {

or better

if (form.position.value.length==0) {
Copy linkTweet thisAlerts:
@compuboy1010authorOct 27.2004 — [i]Originally posted by Kor [/i]

[B]if (!form.position.value) {



In fact anytime you have a value for your inputs, it is ""...



You must use



if (form.position.value=='') {



or better



if (form.position.value.length==0) { [/B]
[/QUOTE]


It works now.

if (!form.position.value)

This works though.

What is the easiest way to test if a field contains a number ?
Copy linkTweet thisAlerts:
@CharlesOct 27.2004 — [i]Originally posted by Kor [/i]

[B]Not a wise condition. In fact anytime you have a value for your inputs, and that is a '' string...



You must use:



if (form.position.value=='') {



or better



if (form.position.value.length==0) { [/B]
[/QUOTE]
Why?

You can put anything inside those parentheses, even an assignment, and it will br evaluated in the Boolean context. In the case of an assignment that would be the value on the right side of the assignment operator. An empty string is evaluated as false.

In Perl one is expected to get out of hand with this sort of thing.
Copy linkTweet thisAlerts:
@KorOct 28.2004 — 
An empty string is evaluated as false.
[/quote]

Korekt... I admit I was wrong... I don't know where my mind was...?


What is the easiest way to test if a field contains a number ?
[/quote]


Using Regular Expresion, I reckon...

[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script language="JavaScript" type="text/JavaScript">
function isNumber(f) {
var re = /^[0-9]*$/;
if (!re.test(f.elements['txt'].value)) {
alert("Value must be all numeric characters, non numeric's removed from field!");
f.elements['txt'].value = f.elements['txt'].value.replace(/[^0-9]/g,"");
return false;
}
}
</script>
</head>
<body>
<form onsubmit="return isNumber(this)">
<input name="txt" type="text">
<input type="submit" value="Submit">
</form>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@CharlesOct 28.2004 — [font=monospace]<script type="text/javascript">

<!--

// [i]If you want to test if a string contains one or more numeric characters then you can use the regular expression special character "d":[/i]

String.prototype.isNumeric = function () {return /d/.test(this)}

// [i]If you want to test if a string contains nothing but numeric characters then you can use the regular expression special character "D" and negate the result:[/i]

String.prototype.isNumeric = function () {return !/D/.test(this)}

// [i]If you want to test if a string is a number then you need only cast it as a Number:[/i]

String.prototype.isNumeric = function () {return Boolean (Number (this))}

// [i]Some prefer, however, to test for NaN and negate the result:[/i]

String.prototype.isNumeric = function () {return !isNaN (this)}

// [i]I'd follow the penultimate example, but I wouldn't use a method. Knowing that casting as a number something that is not a number returns the special value NaN and knowing that after an "if" anything inside the parentheses will be evaluated in the Boolean context I would use:[/i]

if (Number (x)) {}

// [i]That method somehow seems to make sense to me. [/i]

// -->

</script>[/font]
×

Success!

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