Results 1 to 4 of 4

Thread: Checking textbox for illegal characters w/ Javascript

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    492

    Checking textbox for illegal characters w/ Javascript

    I am working on a form, with several text boxes on it. I would like to know how I can go about trying to check for certain characters. Currently I am just checking if the text boxes have something in them. with the following code.

    (isEmpty(document.guestform.visitorname.value.toString()))
    { alert("You need to provide a name.");
    document.guestform.vistorname.focus(); return false; }

    How can I incorportate into this or after this piece of code something that will check if a "\" or a "/" is found. I need to check for this because \ / is not part of a valid name.

    I am using javascript.

    Thanks to all that help out.

    I really appreciate your help.

  2. #2
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655
    Use a regular expression search to get what you want. Try this example (let me know if you need an explanation of what is happening in "searchpattern"):

    Code:
    <script language="JavaScript">
    var searchpattern = /(\\|\/)+/
    
    function CheckString(FString){
    if(FString.search(searchpattern) != -1){
    alert("You can not enter a \\ or a / into the field.")
    }
    }
    </script>
    
    <form name="MyForm">
    <input type="text" name="MyText" value="Hell/0 World!" onblur="CheckString(this.value)">
    <input type="submit">
    </form>
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    492
    RealisticGraphics, thanks for the code. No need to explain, I see what is going on in it.

    Thanks for your help.

  4. #4
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655
    No problem!
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

Posting Permissions

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



Click Here to Expand Forum to Full Width