Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

java script form validation no number or charachters

Posted on 2011-09-26
26
Medium Priority
?
231 Views
Last Modified: 2012-05-12
Looking for a java script that will send an alert message on submit if the input field has a number or charachter
0
Comment
Question by:darrellbuckley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 12
  • 2
26 Comments
 
LVL 11

Expert Comment

by:MacAnthony
ID: 36599629
This is assuming you don't want the form to submit if it contains a number or letter.

<script>
function validateInput(){
    return (document.getElementById('validateField').value.search(/\w/i) == -1); // returns false if it contains a letter or number
}
</script>

<form onsubmit="return validateInput();">
<input id="validateField" value="" />
</form>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 36600014
@MacAnthony
\w include the underscore << _ >>

what do you mean by charachter? a letter?

another one, test page : http://jsfiddle.net/c246B/
<script>
function validateInput(){
    if( /[\da-z]/i.test( document.getElementById('validateField').value )  ) alert("contain NUMBer or letter");
}
</script>

<form onsubmit="return validateInput();">
<input id="validateField" value="" />
<input type="submit" value="check" />
</form>

Open in new window

0
 

Author Comment

by:darrellbuckley
ID: 36600131
Sorry, like a question mark or semi colen.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 82

Expert Comment

by:leakim971
ID: 36600154
function validateInput(){
    if( /[\d!;]/i.test( document.getElementById('validateField').value )  ) alert("contain NUMBer, ! or ;");
}

Open in new window


test page : http://jsfiddle.net/c246B/3/
0
 
LVL 11

Expert Comment

by:MacAnthony
ID: 36600241
Or if you want everything but normal text alphabet characters to be flagged:

(borrowing leakim971's format)
function validateInput() {
    if( /[^a-z ]/i.test( document.getElementById('validatedField').value) ) alert('Contains non-alphabet characters');
}

Open in new window

0
 

Author Comment

by:darrellbuckley
ID: 36600450
Can I use this in multible inputs on the form?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36600473
yes, just change the id to test the right field : validatedField
0
 

Author Comment

by:darrellbuckley
ID: 36600552
i think the code is flagging letters..I only need letter to post no numbers or special charachters, @, &, # ect.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36600562
only letter use : /[a-z]/i
0
 

Author Comment

by:darrellbuckley
ID: 36600745
ok it works but the form still posts to the next page is there a way to make them have to change the form before it posts
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36600761
ok return false to stop submission of the form else return true

function validateInput() {

if( /[a-z ]/i.test( document.getElementById('validatedField').value) )
{
   alert('Contains non-alphabet characters');
   return false;
}

return true;

}
0
 

Author Comment

by:darrellbuckley
ID: 36600865
perfect almost there, how do I add this to other field within code?

first name field
last name field and so on
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36600940
just change the id to test the right field : validatedField

if( /[a-z ]/i.test( document.getElementById('validatedField').value) && /[a-z ]/i.test( document.getElementById('validatedField_2').value) && and so on )
0
 

Author Comment

by:darrellbuckley
ID: 36600966
function validateInput(){
    if( /[\d!;]/i.test( document.getElementById('validateField').value )  )
    {
       alert('Contains non-alphabet characters');
       return false;
    }

    return true;

    }
</script>

this is what i have so far might be different
0
 

Author Comment

by:darrellbuckley
ID: 36601091
like this?

function validateInput(){
    if( /[\d!;]/i.test( document.getElementById('validateField').value )  )
    {
       alert('Contains non-alphabet characters');
       return false;
    }

    if( /[\d!;]/i.test( document.getElementById('validateField2').value )  )
    {
       alert('Contains non-alphabet characters');
       return false;
    }

    return true;

    }
</script>

0
 
LVL 82

Expert Comment

by:leakim971
ID: 36601095
no, just add && /[a-z ]/i.test( document.getElementById('validatedField_2').value)

for any new/other field

replace validatedField_2 by the right ID attrubute avlue
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36601099
>like this?

no like this :

if( /[a-z ]/i.test( document.getElementById('validatedField').value) && /[a-z ]/i.test( document.getElementById('validatedField_2').value) && and so on )
0
 

Author Comment

by:darrellbuckley
ID: 36601187
<script>
function validateInput(){
    if( /[\d!;]/i.test( document.getElementById('validateField').value )  &&  /[\d!;]/i.test( document.getElementById('validateField').value)
    {
       alert('Contains non-alphabet characters');
       return false;
    }
    return true;
    }
</script>

this doesnt seem to work, sorry for the uncertainty
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36601198
a tip :
count the number of ( parentheses
you must have the same number of )

 if( /[\d!;]/i.test( document.getElementById('validateField').value )  &&  /[\d!;]/i.test( document.getElementById('validateField').value)  )
0
 

Author Comment

by:darrellbuckley
ID: 36601221
added ) but still not working

<script>
function validateInput(){
    if( /[\d!;]/i.test( document.getElementById('validateField').value )  &&  /[\d!;]/i.test( document.getElementById('validateField2').value) )
    {
       alert('Contains non-alphabet characters');
       return false;
    }
    return true;
    }
</script>
0
 

Author Comment

by:darrellbuckley
ID: 36601244
what i have now

<script>
function validateInput(){
    if( /[\d!;]/i.test( document.getElementById('validateField').value )  &&  ( /[\d!;]/i.test( document.getElementById('validateField2').value) )
    {
       alert('Contains non-alphabet characters');
       return false;
    }
    return true;
    }
</script>
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36601257
ID:36600562

Contains non-alphabet characters mean you only want a to z?
because you ask for ! and ;
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36601268
run fine for me : http://jsfiddle.net/AKp6f/1/
0
 

Author Comment

by:darrellbuckley
ID: 36601286
now it works, something must have been missing. yes I only want to process a to z nothing else
0
 

Author Comment

by:darrellbuckley
ID: 36601308
no @ or :; % all that stuff
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 36601337
ID:36600562
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question