Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Validate <form> with out action attribute

javascript validate

  • Please log in to reply
11 replies to this topic

#1 dillyw82

dillyw82

    CC Newcomer

  • Member
  • PipPip
  • 15 posts
  • Programming Language:JavaScript
  • Learning:JavaScript

Posted 30 October 2012 - 02:02 AM

Hi i i have a assignment where i validate all forms are correctly filled in by user when click submit but i cant work out how to do it with out setting the action attribute, i have tryed action with "" & "#" ?
  • 0

#2 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 30 October 2012 - 02:13 AM

the form tag wants a method and action. try action="yourfilename" method="get" where "yourfilename" is the filename you actually named your file to.

oh, also, your javascript needs to be called in the onsubmit attribute of the form tag.
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#3 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 30 October 2012 - 02:48 AM

I think the <form> tag does not require the method or action attributes
method should default to get, the action would default to the current page (url).

Very often you want the method to be 'POST' though, the action depends on the design of the website. I personally like staying on the same URL, so when some server validation goes wrong, it easily goes back to the form-page. But this is not required at all.


As for the javascript validation, like Orjan said, you need the onsubmit attribute:



<html>
<head>
  <script type="text/javascript">
        function validateForm(){
          if(field1.isEmpty() ){
                 return false;
          } else if(field2.isEmpty() ) {
                 return false;
          }
          //continue for all fields
          return true; //if it reaches this line, all fields are processed, return true to submit form.
        }
  </script>
</head>
<body>
  <form onsubmit="validateForm()">
          [...]
  </form>
</body>
</html>
Note: field.isEmpty() is pseudo code, such method does not exist by default.
  • 0

#4 dillyw82

dillyw82

    CC Newcomer

  • Member
  • PipPip
  • 15 posts
  • Programming Language:JavaScript
  • Learning:JavaScript

Posted 30 October 2012 - 12:30 PM

Hi thanks you both for your replys i have tryed both but still not getting the alert popping up would you mind taking a look at my file for me please

Attached Files


  • 0

#5 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 30 October 2012 - 02:23 PM

First, let's get rid of the syntax errors :):
Spoiler


Then, why does it not do an alert?
Because your onsubmit method is called 'checkForm', but in the javascrpit it's called 'validate'.
So changing that should make things working.
<form name="input" action="JoesFruit.html" method="get" onsubmit="validate()">

  • 0

#6 dillyw82

dillyw82

    CC Newcomer

  • Member
  • PipPip
  • 15 posts
  • Programming Language:JavaScript
  • Learning:JavaScript

Posted 30 October 2012 - 02:29 PM

oh golly gosh thanks cant believe i missed that, still not working though ?

and i corrected resturn to return still not working
  • 0

#7 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 30 October 2012 - 02:29 PM

Are you sure you press the submit button with the name empty? Becuase if it's filled with 'Please fill your name', it won't be empty and you'll get no alert.

Which browser do you use mainly?
  • 0

#8 dillyw82

dillyw82

    CC Newcomer

  • Member
  • PipPip
  • 15 posts
  • Programming Language:JavaScript
  • Learning:JavaScript

Posted 30 October 2012 - 02:38 PM

i have corrected a few smelling mistakes and changed a few things but still not working i will put up the updated copy

yeah i have emptyed it

i use IE9

just tryed with firefox dont work there either

ok got it not thank you for your time, how did you do that check over the code that come up with the errors?

Attached Files


  • 0

#9 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 30 October 2012 - 02:45 PM

Okay, to help debugging Javascript open your file in IE.
Then press F12, a 'developers tools' window opens.

Now press refresh in the normal IE window.

After that go to the tools window, click on the script tab. And there on the right under 'console' it will list any Javascript error.
Should look like:
Posted Image
It states it's missing a '}' at line 36.

Line 36 is the end of the script part - So what that means is you didn't close a brace where you've opened one.

I've pointed that out as error in my previous post as

} //missing

- still missing that one.

After you've edited the file, and refreshed the browsers you'll see that in the tools window it still shows the error. But that's the error from the previous run, so no worries.
You can avoid this by right clicking where the errors are and click 'Clear Console' BEFORE refreshing the page.

It should be close to error free now.


You can also use the developer tools window to debug javascript. It's a very handy window.

In google Chrome it's also F12 to open a similar tools window
In firefox it's ctrl+shift+k (errors) and ctrl+shift+s (debug)
  • 1

#10 dillyw82

dillyw82

    CC Newcomer

  • Member
  • PipPip
  • 15 posts
  • Programming Language:JavaScript
  • Learning:JavaScript

Posted 30 October 2012 - 03:51 PM

cool thanks u wish my course would teach us things like that :)
  • 1

#11 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 31 October 2012 - 12:39 AM

I wish my course did the same :P. I used to absolutely HATE javascript in the beginning, for the lack of having a way to find errors.
I was putting alert(..) statements on EVERY odd line to find errors, how far does the program run... Quite horrible.

I'm very glad browsers have got (very) decent Javascript debugging tools now (and I know about them).
  • 0

#12 dillyw82

dillyw82

    CC Newcomer

  • Member
  • PipPip
  • 15 posts
  • Programming Language:JavaScript
  • Learning:JavaScript

Posted 31 October 2012 - 01:51 PM

sorry i didnt get any notification for this, yeah im certainly hating it atm but imsure it will all come together one day lol
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download