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()
}
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.