/    Sign up×
Community /Pin to ProfileBookmark

.js file and forms

I’m fairly new to writing web pages & all other web related technology. I have a few jsp pages which have forms. I want them to use the same javascript file (jScript.js) for validation. The code in the js file is as follows, which needs alot of modifications:

[CODE]
var warningColor = “#FF2828”

function validForm(passForm) {
var whiteBGcolor = “#FFFFFF”
passForm.firstname.style.backgroundColor = whiteBGcolor
passForm.lastname.style.backgroundColor = whiteBGcolor
passForm.username.style.backgroundColor = whiteBGcolor
passForm.email.style.backgroundColor = whiteBGcolor
passForm.address.style.backgroundColor = whiteBGcolor
passForm.phone.style.backgroundColor = whiteBGcolor
passForm.password.style.backgroundColor = whiteBGcolor
passForm.confirmpassword.style.backgroundColor = whiteBGcolor

<!–Check for null entries in form–>
if (passForm.firstname.value == “”){
passForm.firstname.style.backgroundColor = warningColor
passForm.firstname.focus()

return false
}

if (passForm.lastname.value == “”){
passForm.lastname.style.backgroundColor = warningColor
passForm.lastname.focus()

return false
}

if (passForm.username.value == “”){
passForm.username.style.backgroundColor = warningColor
passForm.username.focus()

return false
}

if (passForm.email.value == “”){
passForm.email.style.backgroundColor = warningColor
passForm.email.focus()

return false
}

if (passForm.address.value == “”){
passForm.address.style.backgroundColor = warningColor
passForm.address.focus()

return false
}

if (passForm.phone.value == “”){
passForm.phone.style.backgroundColor = warningColor
passForm.phone.focus()

return false
}

if (passForm.password.value == “”){
passForm.password.style.backgroundColor = warningColor
passForm.password.focus()

return false
}

if (passForm.confirmpassword.value == “”){
passForm.confirmpassword.style.backgroundColor = warningColor
passForm.confirmpassword.focus()

return false
}

<!–Check to see if passwords are same –>
if (passForm.password.value != passForm.confirmpassword.value){
alert(“Passwords do not match”)

passForm.password.value = “”
passForm.password.style.backgroundColor = warningColor
passForm.confirmpassword.value = “”

passForm.password.focus()

return false
}

if (!checkMail(passForm)){
return false
}

if (!checkString(passForm)){
return false
}

<!– if (!checkAddress(passForm)){–>
<!– return false–>
<!– }–>

if (!checkPhone(passForm)){
return false
}

return true
}

<!–Check entered email address for correct syntax–>
function checkMail(passForm){

var strEmail = passForm.email.value;

<!– Explanation of RegEx check –>
<!– Scan the entire value from the beginning (^) to ($): –>
<!– User Name part, i.e. before the @, should only include alphanumerical (letters or numbers), –>
<!– underscores, dots or dashes values – ([a-zA-Z0-9_.-])+ –>
<!– next comes the @ symbol – @ –>
<!– After that, the domain name, which may include several sub-domains (com.cit). –>
<!– Several series of alphanumerical characters and dashes, followed by a dot will be allowed. –>
<!– (([a-zA-Z0-9-])+.)+ –>
<!– Finally the top level(.com, .co.uk, .ie) domain, once again we check for
<!– alphanumerical characters, but now without the dash. Must be between 2 and 4 characters. –>
<!– ([a-zA-Z0-9]{2,4})+$/; –>

var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

if (!filter.test(strEmail)){
passForm.email.value = “”
passForm.email.style.backgroundColor = warningColor
passForm.email.focus()

return false
}

return true
}

function checkString (passForm){

var strString = passForm.firstname.value;

var filter = /^([a-zA-Z\s]{1,25})[a-z]$/;

if (!filter.test(strString)){
passForm.firstname.value = “”
passForm.firstname.style.backgroundColor = warningColor
passForm.firstname.focus()

return false
}

return true
}

<!–function checkAddress (passForm){–>

<!– var strString = passForm.address.value;–>

<!– var filter = /([0-9a-zA-Z\s[,.]]+|[a-zA-Z[,.]][a-zA-Z[,.]]+)/;–>

<!– if (!filter.test(strString)){–>
<!– passForm.address.value = “”–>
<!– passForm.address.style.backgroundColor = warningColor–>
<!– passForm.address.focus()–>

<!– return false–>
<!– }–>

<!– return true–>
<!–}–>

function checkPhone (passForm){

var strString = passForm.phone.value;

var filter = /^([0-9]{3,11})$/;

if (!filter.test(strString)){
passForm.phone.value = “”
passForm.phone.style.backgroundColor = warningColor
passForm.phone.focus()

return false
}

return true
}

<!– clear all the form fields –>
function clearForm (clsForm) {
clsForm.reset()
}
[/CODE]

What I did is write a jsp page called adduser and the decided to use a jsp page so I just copied the script from adduser into jScript.js. How do I make it so other forms can use this js file, i.e. how can I specify the element? I think what I want is an array so I can loop through an test each element but I’ll need do certain tasks for certain elements, e.g. email & checkmMail(passform).

Any ideas?

Thanks in advance.

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @rebels_mascot 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.6,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...