Jump to content

Recommended Posts

Hi,

 

I've made a form that validate the name, email address, etc. So, if a visitor submit without filling their name, an alert box will appear and the visitor must klik ok and fill in the name field. It works properly on opera and ie, but not firefox. It always return true, and go to the next page. Why?

 

This is the javascript code:

<script type="text/javascript">
function valid(){
var check_lname= document.myform.lname.value
var check_email= document.myform.email.value
var check_check= document.myform.agree.checked

if (check_lname==''){
	alert('name required')
	myform.lname.focus()
	return false
	}

if ((check_email=='') || (check_email.indexOf('@',0) == -1) || (check_email.indexOf('@',0) == -1)){
	alert('valid email please')
	myform.email.focus()
	return false
	}

if (check_check==''){
	alert('You must agree.')
	myform.agree.focus()
	return false
	}
	return true
}

</script>

<form method='post' name='myform' action='confirm.php' onSubmit='return valid()'>

 

Could anyone help?

 

Thanks

ayok

 

Link to comment
https://forums.phpfreaks.com/topic/92721-validation-doesnt-work-on-firefox/
Share on other sites

i think you are going about it the wrong way. I'll show you a cleaner way to do it.

 

function validateForm(){
var form = document.getElementById('form_unique_id');
form.onsubmit = function(){
	//do your checks in here "this" is the form element. so you can say this.elements['element name'].value;
	var ele1 = this.elements['ele1'].value;
	var error = false;
	if(ele1 == ''){
		error = true;
	}

	if(error){
		//do whatever
		return false;
	}else{
		this.submit();
	}
};
}

window.onload = validateForm;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.