Milos Sretin
Milos Sretin

Reputation: 1748

Restrict input some personal information

I want to check what users type in Textarea.

Actually, how can I restrict typing phone numbers and e mail addresses in description box?

So for example:

Hi, I am selling a Bugatti Veyron
Age: 2010
Color: Black
You can contact me on 066/656-656 or 055646646
or via mail [email protected]

If someone tries to enter something like this I want to automatically remove personal contact details.

So, please help, how can I do it?

Upvotes: 0

Views: 662

Answers (3)

SauriolJf
SauriolJf

Reputation: 412

Use Regex and do something like this :

Here's an example (jsFiddle)

HTML

<textarea></textarea><br>
<button>Test</button><br>
<span class="result"></span>

Javascript:

function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    $('.result').html(re.test(email));
    if(re.test(email)){
        $('.result').html("Contain email");
    } else {
        $('.result').html("Do not contain email");
    }
}

$('button').click(function(){
    validateEmail($('textarea').val());
});

Note that I only look for email. But you can use other Regex to look for phone, you just have to search for something like "javascript regex phone" on Google.

Upvotes: 1

Patrick
Patrick

Reputation: 3367

you can try Regex as suggested, but take into consideration it's very hard to stop a phone number from being entered(unless you stop ALL numbers or know the exact form of the number taking place).

For example, stopping a xxx-xxxxxxx number is easy, but the user can type each digit with a space after it, which makes it much harder to stop(unless you again remove the option to type numbers.

As for emails, a simple regex to find a @ followed by some text, a dot and 2 or 3 characters normally finds emails pretty easily. be advised people can still be creative in the way they put their emails(AT instead of @ for example).

this should all be done server side, you can use javascript on the clientside to make it UI friendly.

Upvotes: 1

Ashima
Ashima

Reputation: 4824

For email validation:

<input type="email" name="email">

Here is an example: http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_input_type_email

For Phone number validation: use regex, it should be straight forward though. Or just try isNAN(value) where value is the phone number enterd, will tell you if this is a number of not and then you can also put a check on number of digits.

Hope this helps!

Upvotes: -1

Related Questions