[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

java script form validation no number or charachters

Looking for a java script that will send an alert message on submit if the input field has a number or charachter
0
darrellbuckley
Asked:
darrellbuckley
  • 12
  • 12
  • 2
1 Solution
 
MacAnthonyCommented:
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
 
leakim971PluritechnicianCommented:
@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
 
darrellbuckleyAuthor Commented:
Sorry, like a question mark or semi colen.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
leakim971PluritechnicianCommented:
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
 
MacAnthonyCommented:
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
 
darrellbuckleyAuthor Commented:
Can I use this in multible inputs on the form?
0
 
leakim971PluritechnicianCommented:
yes, just change the id to test the right field : validatedField
0
 
darrellbuckleyAuthor Commented:
i think the code is flagging letters..I only need letter to post no numbers or special charachters, @, &, # ect.
0
 
leakim971PluritechnicianCommented:
only letter use : /[a-z]/i
0
 
darrellbuckleyAuthor Commented:
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
 
leakim971PluritechnicianCommented:
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
 
darrellbuckleyAuthor Commented:
perfect almost there, how do I add this to other field within code?

first name field
last name field and so on
0
 
leakim971PluritechnicianCommented:
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
 
darrellbuckleyAuthor Commented:
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
 
darrellbuckleyAuthor Commented:
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
 
leakim971PluritechnicianCommented:
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
 
leakim971PluritechnicianCommented:
>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
 
darrellbuckleyAuthor Commented:
<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
 
leakim971PluritechnicianCommented:
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
 
darrellbuckleyAuthor Commented:
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
 
darrellbuckleyAuthor Commented:
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
 
leakim971PluritechnicianCommented:
ID:36600562

Contains non-alphabet characters mean you only want a to z?
because you ask for ! and ;
0
 
leakim971PluritechnicianCommented:
run fine for me : http://jsfiddle.net/AKp6f/1/
0
 
darrellbuckleyAuthor Commented:
now it works, something must have been missing. yes I only want to process a to z nothing else
0
 
darrellbuckleyAuthor Commented:
no @ or :; % all that stuff
0
 
leakim971PluritechnicianCommented:
ID:36600562
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 12
  • 12
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now