Check letters, numbers and special characters

Hi there.

I need ur appreciated help.

This is the function CheckmyForm.

I need check that you enter only letters, numbers and special characters “,”, “.” and “;” in the TextareaS1 field.

Can u help me?

function CheckmyForm() {

   if ( myform.DatesT1.value.length > 0 && myform.TextareaS1.value.length > 0 ) {
        alert("OK!.");

    } else     {

        alert("KO!.");
        myform.DatesT1.focus();

    }

}

Here’s your student-proof solution:

<form action = '#' method = 'post' onsubmit = "return checkForm( this )">
 <input name='data'>
 <input type='submit'>
</form>
<script type='text/javascript'>

function checkForm( theForm )
{
  var result = /[^a-z0-9\\,\\.\\;]/g( theForm.data.value );

  if( result )
  {
    alert('One or more illegal characters were found, the first being character ' + ( result.index + 1 ) + ' "' + result +'".\
\
Please edit your input.');
  }

  return !result;
}
</script>

Thank u Sir.

But I have one question for u.

What difference with this regExp:

/[^a-z0-9\\,\\.\\;]/g,

and this regExp

"^[a-zA-Z0-9\\.;,:' ]{1,100}$"

?

One of them has uppercase A-Z as well, and has had added to it colon quote space.
The numbers in curly braces indicate the min/max characters that will be matched.

The ^ inside the square brackets indicates that those characters much not match
Whereas, the ^ at the start, with the $ at the end, indicates that what’s in-between must match the entire string.

The first matches any unacceptable characters, the second matches a string of 1-100 acceptable characters.

Thank u Sir.

But if write this:

var re = [^a-z0-9\\,\\.\\;]/g

ur script working.

If write

/^[a-zA-Z0-9\\.;,:' ]{1,100}$/g

ur script not working…

Why?

Javascript error: ‘index’ Is Null or Not an Object
Line: 29
Character: 11
Code: 0
URI: http://localhost/xml/_checkbox/pagenew.htm

checking for characters should be easy…

if( !/[\w\s\;\:\.\,]+/gi.test( WhatEver.Value ) )
{
alert( "Check Failed, please, fix it! " );
return;
}

not tested but that would check

“\w” ( alphanumerics )
“\s” ( spaces )

and whatever other characters you want after that.
make sure to change “WhatEver.Value” with the string to be tested.

I omitted to add the i flag to the expression, but could you show the full code that doesn’t work?

Ok (my browser is IE 8):

<form action = '#' method = 'post' onsubmit = "return checkForm( this )">
 <input name='data'>
 <input type='submit'>
</form>

<script type='text/javascript'>

function checkForm( theForm )
{
  var result = /^[a-zA-Z0-9\\.;,:' ]{1,100}$/g( theForm.data.value );  
 
  if( result )
  {
    alert('One or more illegal characters were found, the first being character ' + ( result.index + 1 ) + ' "' + result +'".\
\
Please edit your input.');  
  }
   
  return !result; 
}
</script>

Your expression represents a required match so the logic is different:

function checkForm( theForm )
{
  var result = /^[a-z0-9\\.;,:'\\s]{1,100}$/i( theForm.data.value );

  if( !result )
  {
     alert("No legal characters entered");
  }

  return !!result;
}

Sorry Sir, but ur code not working…

<form action = '#' method = 'post' onsubmit = "return checkForm( this )">
 <input name='data'>
 <input type='submit'>
</form>
 
<script type='text/javascript'> 
 
function checkForm( theForm )
{
  var result = /^[a-z0-9\\.;,:'\\s]{1,100}$/i( theForm.data.value );  
 
  if( !result )
  {
     alert("No legal characters entered");
  }
   
  return !!result; 
}
</script>

Sorry Sir, but ur code not working… are u tested?

<form action = '#' method = 'post' onsubmit = "return checkForm( this )">
 <input name='data'>
 <input type='submit'>
</form>
 
<script type='text/javascript'> 
 
function checkForm( theForm )
{
  var result = /^[a-z0-9\\.;,:'\\s]{1,100}$/i( theForm.data.value );  
 
  if( !result )
  {
     alert("No legal characters entered");
  }
   
  return !!result; 
}
</script>

I have run up a test of the code that was posted and it seems to work. What seems to be the problem?

Do you have any test inputs that are not working as expected?
Or is something more serious occurring. If so, something might be conflicting that we can resolve by taking a look.

The form is validate even with invalid characters in the input name=‘data’.

What characters do you consider to be invalid?

All characters differents by letters, numbers and ’ , ; . : and the backspace.

For example ? is invalid, ^ is invalid, / is invalid, * is invalid, etc.

It seems to work on the test code that I created from your post, so assuming that your test code is the same, there must be something else causing the problem.

Can you help me to take a look at your test page? Commonly the most effective way is for you to link to a test page that you have on the internet, or failing that, you can attach a file to a post.

By the way. The script will only run when scripting is actually enabled.

If you’re testing a local page on IE, be sure to enable scripting. IE doesn’t enable scripting on local web pages until you choose to enable it from the yellow info bar.
See for an example: http://www.troublefixers.com/disable-activex-warning-in-internet-explorer-while-playing-animations/

Dear friend, try it yourself:
http://www.avia-it.com/testaruba/_check.htm

This code not working… :frowning:

What do you mean by “not working”

[list][]Does the form submit to a blank page?
[
]Does the form submit to an error page?
[]Does the alert appear when it shouldn’t?
[
]Does your computer explode at random?
[/list]

Can you please break this down in to steps that I can follow to try and duplicate your experience. For example:

[list][]what you enter
[
]what you expect to occur
the problem that actually occurs[/list]

From what I’m getting from you right now, I believe that you do not have scripting active on your web browser for this page.

Thanks for ur help.

I confirm: this script not working and all javascript in my computer work properly without problems.

The form validate even with invalid characters in the input name=‘data’.

| = validate
\ = validate
! = validate
£ = validate
$ = validate
% = validate
& = validate
/ = validate
( = validate
) = validate
= = validate
? = validate
^ = validate

  • = validate
    [ = validate
    ] = validate
  • = validate
  • = validate
    _ = validate
    @ = validate

= validate

§ = validate
° = validate
ç = validate

But it’s not important, I can find other solution for my problem.

Regards and thanks for ur time.