0

How can write this on javascript. This is on html form.

<form action="@Url.Action("NewPage")" >
 ....
</form>

Now I have javascript function.

function validateForm() {
     //var x = document.forms["form"]["fname"].value;
     var x = document.getElementById('id').value;
     if (x == null || x == 0 || x == "0") {
         alert("stop");
         return false;
     }
     else {
         document.form.submit();


     }
}

What should be in the html form action.

8
  • 3
    Write this in JavaScript where? What are you asking? Commented Aug 28, 2013 at 6:17
  • <a href="NewPage.html">Click event for page</a>. Normally this is used to navigate to new html page. Please provide more details about your requirement. Commented Aug 28, 2013 at 6:19
  • 3
    Hello, asp.net MVC... Is that you there, not being tagged? Commented Aug 28, 2013 at 6:19
  • I update. Here is function. Commented Aug 28, 2013 at 6:21
  • ok,you want that in else condition? Commented Aug 28, 2013 at 6:22

4 Answers 4

0

Try this

action='@Url.Action("ActionName","ControllerName")';
Sign up to request clarification or add additional context in comments.

Comments

0

EDIT: It now looks like you want to have your HTML form redirect to another page on submit. To accomplish that, do this:

<form action="newurl.php" method="post">
[...]
</form>

where newurl.php is whatever page you want to submit to.


ORIGINAL ANSWER:

If you want to use javascript to change the url, which is what it looks like you want to do, use this:

<script type="text/javascript">
    location.href="http://new.url/whatever";
</script>

If you want to do it on a button-press, wrap it in a function and have the button call the function:

<script type="text/javascript">
function newURL(url) {
    location.href=url;
}
</script>
<input type='button' value='Google' onclick="newURL('http://google.com')" />

Comments

0

For redirection,Use any these

    - alert(document.URL)

    - alert(Window.location.href)

    - alert(document.location.href)

So to redirect,use this function and call it appropriately:

 function validateForm() {
 //var x = document.forms["form"]["fname"].value;
 var x = document.getElementById('id').value;
 if (x == null || x == 0 || x == "0") {
     alert("stop");
     return false;
 }
 else {
     document.form.submit();
     Window.location.href="www.google.com";//redirect on form submit
 }

}

Note:If you have an action page,then give it a header.

Comments

0

try this.

<script type="text/javascript">
function DoRedirect() {
window.location.href = 'http://www.google.com';
}
</script>

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.