0

I have the following:

<input class="required" name="nifpasaporte" id="nifpasaporte" type="text" placeholder="<?php echo JText::_('COM_CSTUDOMUS_NIFPASAPORTE');?>" value="<?php echo $userData->nifpasaporte ?>" required="required" aria-required="true" tabindex="7">

Using jQuery, how could I create a function which validates this input box only allowing to enter text and numbers (NO SPECIAL CHARACTERS).

For example:

function isnumeric(valor){
valor=valor.replace(",",".");
if (valor.match(/^[0-9]*\.*[0-9]+$/))
    return true;
else
    return false;
} 
7
  • are you getting any errors with this function? Commented Mar 18, 2014 at 10:24
  • 1
    "Only text and numbers"... So... everything?! Commented Mar 18, 2014 at 10:25
  • Im not sure how this is related to PHP except that PHP is the only relieable language for validating user input. Commented Mar 18, 2014 at 10:25
  • @deceze i suppose he wants to prevent special chars like $ or Commented Mar 18, 2014 at 10:26
  • 1
    Assuming you mean "alphanumeric" instead of "text & numbers", then just search for alphanumeric validation. Plenty of examples. Keep in mind that javascript validation is really only aesthetic, it'll keep unintentional input from being entered, but it is by no means a good validation method, and anyone that really wants to input otherwise, will be able to. Always validate the input serverside as well (if you're not doing so already). Commented Mar 18, 2014 at 10:28

3 Answers 3

1

try this code:

// creates a function when the form is submitted
$('form').submit(function(){

    // your string to check
    var str = "mystring001";

    // match function checks if your string contains ONLY text and/or numbers (alphanumeric)
    if (str.match(/[^a-zA-Z0-9]/) {

        // if match
        return true;
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Explaining what the code is doing might be helpful.
added a few comments, btw, he asked for a function not to accept special characters, this will help him and code is pretty clear.
1

Nowadays html5 has got validating properties in itself. For example, if you want to check whether the content entered in email input field is correct or not, just change the type="text" to type="email". use required="required". Here's a JavaScript function that does the same job. Simple, Fast & Easy.

function isNumberKey(evt)
{
     var charCode = (evt.which) ? evt.which : event.keyCode
     if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

     return true;
  }

The above given function only allows the user to input only numbers. No special characters & alphabets. It allows vertical tab & backspace. If you want user to input alphabets also, just extend the condition, use ASCII values of alphabets. I think its b/w 65 to 92 and 97 to 122..something like that.

Comments

0

Create a handler on the submit event of your form

$('form').submit(function(){
  // your validations

  if (!pass)
    return false; // and prevent form submit
  else
    return true;  // continue with form submit
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.