www.webdeveloper.com
Results 1 to 9 of 9

Thread: Unable to call function from after adding js file

  1. #1
    Join Date
    Aug 2013
    Posts
    31

    Unable to call function from after adding js file

    Hi,

    I am new to this technology, I had created rules.js file in which i mentioned validate function which is used for required field validator for textarea. but after adding the js file in my html page i unable to call that function please let me know what i am doing wrong.


    rules.js file

    Code:
    <script>
       
       function validate()
       {
          var req=document.forms["registration"]["comments"].value;
          
          if (req==null || req=="")
          {
             alert("");
             return false;
          }
       }
    </script>
    HTML Page

    Code:
    <script type="text/javascript" src="rules.js"></script>
    Form Tag
    Code:
    <form id="registration" name="registration" method="POST" action="" onsubmit="return validate();">
    If i add this function on the same HTML page, the after i don't face any problem to calling it, but i want to call it from source file.

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    .js files contain only script statements. Use the error console.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Aug 2013
    Posts
    31
    @logic ali

    It will be more good for me and others if you explain with example , its hard for me to understand.

  4. #4
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by Ashish Sood View Post
    @logic ali

    It will be more good for me and others if you explain with example , its hard for me to understand.
    Delete the <script> tags in the .js file.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  5. #5
    Join Date
    Apr 2008
    Location
    Mumbai, India
    Posts
    255
    Your rules.js file should contain only the function; You should not include the code in .js files with <script>... </script> tags when you call in .js files

    Code:
       function validate()
       {
          var req=document.forms["registration"]["comments"].value;
          
          if (req==null || req=="")
          {
             alert("");
             return false;
          }
       }
    Hope this is very clear

  6. #6
    Join Date
    Aug 2013
    Posts
    31
    Is i am doing right actually i am trying to validate 3 text fields, but its not working.

    the validate function is working fine with one text field. Code is below

    function validate() {

    var inc=document.forms["form1"]["incoming"].value //text field
    var out=document.forms["form1"]["outgoing"].value //text field
    var com=document.forms["form1"]["comments"].value //text area

    if (inc==null || inc=="" || out==null || out=="" || com==null || com="" ) {

    alert("All fields are mandatory");
    return false;
    }
    Please tell me where is am wrong.

    Thanks

  7. #7
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,637
    Quote Originally Posted by Ashish Sood View Post
    ...
    Please tell me where is am wrong....
    you are wrong at this point:

    Code:
    com="" ) {
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  8. #8
    Join Date
    Aug 2013
    Posts
    31
    Thanks alot Padonak

    It was a typo mistake, Now its working

    Thanks

  9. #9
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Minimal validation consists of testing for non-whitespace characters. Testing for a non-empty string is inadequate:

    Code:
    function validate() 
    {
       var inc=document.forms["form1"]["incoming"].value  //text field
       var out=document.forms["form1"]["outgoing"].value //text field
       var com=document.forms["form1"]["comments"].value //text area
       
       var rxp = /\S/, ok = true;
    
       if ( !rxp.test( inc ) || !rxp.test( out ) || !rxp.test( com ) ) 
       {
         alert("All fields are mandatory");
         ok = false;
       }
    
       return ok; /* A function should return a value either always or never, but not sometimes. */
    }
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles