SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    a few questions regarding javascript

    hello every1, i need help with the following questions:

    1) I have an upload form, before the image is being upload i want to check if that image really is an image, how to do it?

    2) i got a foldier containing images for my gallery page, while going through each file in that foldier i want also to check if that file is an image, how to do that aswell?

    3) i got a form that can update a certain date in 1 of my pages, how can i check if the uploaded text really is a date (ie 20/05/2010)

    4) i got a form that can upload text to DB, i dont want this text to contain more than 1 word, how do i check that this text isnt more than a word?

    Thanks for all the help in advance

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,608
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ulthane View Post
    1) I have an upload form, before the image is being upload i want to check if that image really is an image, how to do it?
    You can't - there is no way to examine the file until it reaches the server.

    Quote Originally Posted by ulthane View Post
    2) i got a foldier containing images for my gallery page, while going through each file in that foldier i want also to check if that file is an image, how to do that aswell?
    JavaScript can't access files so how you do that depends on what server side language you use to process them on the server.

    Quote Originally Posted by ulthane View Post
    3) i got a form that can update a certain date in 1 of my pages, how can i check if the uploaded text really is a date (ie 20/05/2010)
    See http://javascript.about.com/library/bldate.htm

    Quote Originally Posted by ulthane View Post
    4) i got a form that can upload text to DB, i dont want this text to contain more than 1 word, how do i check that this text isnt more than a word?
    if fieldname.match(/^\S+$/) is true then fieldname doesn't contain any whitespace (if you want to restrict it to letters and numbers instead of any non-whitespace use \W instead of \S
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    if fieldname.match(/^\S+$/) is true then fieldname doesn't contain any whitespace (if you want to restrict it to letters and numbers instead of any non-whitespace use \W instead of \S
    hmm, this thing doesn't work out for me, i've tried adding it to my javascript code but the code seems to just skip it without checking...did i wrote it right?

    Code:
     if (document.mdogs.desc1.match(/^\S+$/) = false) {
                  alert("this text cant contain more than 1 word");
                  return false;
     }
    Thanks in advance

  5. #5
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to use the field value, not the field itself. Also, comparison for equality in JavaScript uses two equals signs. You're trying to assign false to a function return value.

    Code:
    if (document.mdogs.desc1.value.match(/^\S+$/) == false) {
    Birnam wood is come to Dunsinane

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    See the ASP forum for background on this application.
    Ian Anderson
    www.siteguru.co.uk

  7. #7
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hmm, it still appears to skip that line even after the changes

    Code:
              if (document.mdogs.desc1.value.match(/^\S+$/) == false) {
                  alert("this text cant contain more than 1 word");
                  return false;
              }
    all of my JS codes looks similar to this and they do work, so i cant really find out whats wrong

  8. #8
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Add a couple of diagnostic messages just before that code (if you don't use a browser with built-in development tools or Firefox with the Firebug extension).

    Code:
    alert(document.mdogs.desc1.value);
    alert(document.mdogs.desc1.value.match(/^\S+$/));
    I never use that way of accessing elements myself, so I probably can't be of any assistance.
    Birnam wood is come to Dunsinane

  9. #9
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    i treid it and tested, when i type "a" it alerts "a" and then again "a"
    then i tried with a space like "a a", first time it alerts "a a" and second time it alerts "null"...so i cant really get it, are there any other ways for checking if certain text doesn't contain more than 1 word?

    Thanks

  10. #10
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well i've found an other way that seems to work fine...if soem1 else is intrested here it is...

    Code:
        var anglitstr = ' '   <---- as u can see there's space between the ' ' symbols
              for (var i = 0; i < anglitstr.length; i++) {
                  var anglitchar = anglitstr.charAt(i);
                  if (document.mdogs.desc1.value.indexOf(anglitchar) != -1) {
                      alert("text cannot contain more than 1 word");
                      document.mdogs.desc1.focus();
                      return false;
                  }
              }

  11. #11
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If it is returning null, then you could try the match method like so:

    Code:
    if (document.mdogs.desc1.value.match(/^\S+$/) == null) {
    Ian Anderson
    www.siteguru.co.uk

  12. #12
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks, this one is working

  13. #13
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,907
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    Since you're not interested in backreferences but merely want to know if the string matches the pattern you should proably use test() instead of match():

    Code:
    if (document.mdogs.desc1.value.test(/^\S+$/) == false) {
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  14. #14
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hey again all,
    i got another question, until now i've been working with JS only with forms, now i got a Textarea in my page, i want to make a few checks on that textarea when the form is being submitted, and since its not related to a form, i dont know how to do that

    here's the codes...
    Code:
    <textarea rows="4" cols="20" name="desc1">...</textarea>
    JS code
    Code:
     <script type="text/javascript" language="javascript">
          function checkfrm() {
              if (document.mdogs.desc1.value == "") {
                  alert("יש למלא את תיאור חיית המחמד");
                  document.mdogs.desc1.focus();
                  return false;
              }
              var anglitstr = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
              for (var i = 0; i < anglitstr.length; i++) {
                  var anglitchar = anglitstr.charAt(i);
                  if (document.mdogs.desc1.value.indexOf(anglitchar) != -1) {
                      alert("יש לכתוב תיאור חיית המחמד בעברית");
                      document.mdogs.desc1.focus();
                      return false;
                  }
              }
              var sofitstr = 'כצמפנ'
              for (var i = 0; i < sofitstr.length; i++) {
                  var sofitchar = sofitstr.charAt(i);
                  if (document.mdogs.desc1.value.charAt(document.mdogs.desc1.value.length - 1) == sofitchar) {
                      alert("תיאור חיית המחמד חייב להסתיים עם אות סופית");
                      document.mdogs.desc1.focus();
                      return false;
                  }
              }
              var anglitstr = '!@#$&#37;^&*)(_+|\=-1234567890/][}{;:<,>?.'
              for (var i = 0; i < anglitstr.length; i++) {
                  var anglitchar = anglitstr.charAt(i);
                  if (document.mdogs.desc1.value.indexOf(anglitchar) != -1) {
                      alert("אסור שיהיו סימנים בתיאור חיית המחמד");
                      document.mdogs.desc1.focus();
                      return false;
                  }
              }
              return true;
          }
      </script>

  15. #15
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    A textarea is a FORM ELEMENT, therefore it SHOULD be related to a form. (i.e. contained within the <form></form> tags).

    You access it just the same as a text input form element.

    You might find this of interest. http://www5.brinkster.com/hiflyer/js...mchecktest.htm
    Ian Anderson
    www.siteguru.co.uk

  16. #16
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,608
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    you should proably use test() instead of match()
    regexp.test(string) and string.match(regexp) both do the same thing - it is just which of the regular expression and the string goes where that differs between them.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  17. #17
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,907
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by felgall View Post
    regexp.test(string) and string.match(regexp) both do the same thing - it is just which of the regular expression and the string goes where that differs between them.
    You mean .test() is actually implemented as follows?

    Code:
    function test(str) {
      return (this.match(str) == null);
    }
    It seems to me test() can be implemented more efficiently than match() because test() can stop and return false as soon as it finds one single character that does not match the pattern (lazy evaluation), whereas match() has to check the whole string to see if there is a part of that string that does match the pattern (greedy evaluation).

    At least, that's how I would implement it if I ever were to write a javascript VM (which I'm not) ...
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  18. #18
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,608
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    It seems to me test() can be implemented more efficiently than match() because test() can stop and return false as soon as it finds one single character that does not match the pattern (lazy evaluation), whereas match() has to check the whole string to see if there is a part of that string that does match the pattern (greedy evaluation).
    I wasn't commenting on the efficiency of either but simply pointing out that if you are going to use test() then you have to attach it to the regular expression and pass in the string. You can't attach test() to a string because there is no such string method.

    so

    Code:
    if (document.mdogs.desc1.value.test(/^\S+$/) == false) {
    will not work because document.mdogs.desc1.value isn't a regular expression and /^\S+$/ isn't a string. You'd need to code it as:

    Code:
    if (/^\S+$/.test(document.mdogs.desc1.value) == false) {
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  19. #19
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,907
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    Oops

    You're absolutely right, my bad ...
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


Bookmarks

Posting Permissions

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