1

I have a problem with Javascript functions. My function won't work, I called it in a form with the onsubmit() call but nothing happens.

$(document).ready(function(){
	window.provera = function(){
		nizError = new Array();

		var ime = $("#imeReg").val();
 		var reIme = /^[A-Z][a-z]{2,9}$/;

 		var prezime = $("#prezimeReg").val();
 		var rePrezime = /^[A-Z][a-z]{2,14}(\s[A-Z][a-z]{2,14})*$/;

 		var username = $("#usernameReg").val();
 		var reUsername = /^([A-Za-z]+[0-9]|[0-9]+[A-Za-z])[A-Za-z0-9]*$/;

 		var email = $("#emailReg").val();
		var reEmail =/^[\w]+[\.\_\w]*\@[\w]+([\.][\w]+)+$/;

 		var password = $("#passwordReg").val();
 		var rePassword = /^[\S]{5,50}$/;

		if(!reIme.test(ime)){
			nizError.push('Pogresno ste uneli ime');
		}else if(ime = ""){
			nizError.push('Ime je obavezno da se unese');
		}

		if(!rePrezime.test(prezime)){
			nizError.push('Pogresno ste uneli prezime');
		}else if(prezime = ""){
			nizError.push('Prezime je obavezno da se unese');
		}

		if(!reUsername.test(username)){
			nizError.push('Pogresno ste uneli username');
		}else if(username = ""){
			nizError.push('Username je obavezno da se unese');
		}

		if(!reEmail.test(email)){
			nizError.push('email');
		}else if(email = ""){
			nizError.push('Email je obavezno da se unese');
		}

		if(!rePassword.test(password)){
			nizError.push('password');
		}else if(password = ""){
			nizError.push('Password je obavezno da se unese');
		}

		var ispis = "";

		if(nizError.length > 0){
			for(var i = 0; i < nizError.length; i++){
				ispis+= "<p>"+ nizError[i] +"</p>";
			}
			$("#result").html(ispis);
			return false;
		}
		return true;
	}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="tableLogin">
			<form action="#" method="post" onsubmit="provera()">
				<tr>
					<td>
						<label>Ime</label>
					</td>
					<td>
						<input type="text" id="imeReg" name="imeReg" class="typeText"/>
					</td>
				</tr>
				<tr>
					<td>
						<label>Prezime</label>
					</td>
					<td>
						<input type="text" id="prezimeReg" name="prezimeReg" class="typeText"/>
					</td>
				</tr>
				<tr>
					<td>
						<label>Username</label>
					</td>
					<td>
						<input type="text" id="usernameReg" name="usernameReg" class="typeText"/>
					</td>
				</tr>
				<tr>
					<td>
						<label>E-mail</label>
					</td>
					<td>
						<input type="text" id="emailReg" name="emailReg" class="typeText"/>
					</td>
				</tr>
				<tr>
					<td>
						<label>Password</label>
					</td>
					<td>
						<input type="password" id="passwordReg" name="passwordReg" class="typeText"/>
					</td>
				</tr>
				<tr>
					<td colspan="2">
						<input type="submit" value="Registruj se" id="btnSubReg" name="btnSubReg" class="buttonSub"/>
					</td>
				</tr>
			</form>
		</table>

When I leave # on action attribute nothing happens, but when I try to put some page like admin.php it goes to that page and again I can't validate my form.

I'm working on a website for my studies and I need to validate a form on the client side so if everything is okay it goes on to ther server side where I can again validate it with php and send to the database.

7
  • Add your page.php link for your action attribute. When your event handler (provera) for onsubmit is invoked, call event.preventDefault if the client-side validation fails. preventDefault will prevent the submit/page navigation from occurring. Commented Mar 21, 2019 at 2:06
  • [email protected] is a valid email address but this form prevents that from being used. Commented Mar 21, 2019 at 2:19
  • Try linking in with $('form').submit(function() { ... }) and include your function there to be sure it runs on submit. This is a more concise way of linking it in than this method of shoving it into window first. It's probable you're using the function before its defined with your approach. Commented Mar 21, 2019 at 2:21
  • It's also conventional to just say nizError = [ ] instead of the explicit and much more verbose new Array(). Commented Mar 21, 2019 at 2:27
  • Thank you on the tips @tadman but it wont work again Commented Mar 21, 2019 at 2:29

2 Answers 2

1

Instead of this:

   <input type="submit" value="Registruj se" 
     id="btnSubReg" name="btnSubReg" 
     class="buttonSub"/>

Use this:

    <input type="button" value="Registruj se" 
   id="btnSubReg" onclick="provera()" name="btnSubReg" 
    class="buttonSub"/>

And in your javascript add at the end if everything is valid this:

  document.getElementById("myForm").submit();

And add in "action" your php file path.

Sign up to request clarification or add additional context in comments.

3 Comments

I done everything as you said and again nothing. It should show me text error from nizGreske because I leave everything in form empty, but nothing happens
This code is using jQuery so $('#myform').submit() should suffice.
You should give your form an id for example id="register"
0

You are not calling the OnSubmit() function <td colspan="2"> <input type="submit" value="Registruj se" id="btnSubReg" name="btnSubReg" class="buttonSub" onsubmit="return upload(this)"/> </td>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.