0

I have a school assignment where I'm designing a web page to include form entry. One of the parameters of the assignment is to validate a field where a user enters a name.(must be non-blank, must begin with a capital letter, and must contain at least one vowel and one consonant). As I'm learning the HTML part and not the JavaScript part, I'm allowed to use anything I find for the JavaScript so long as I cite the source. Can anyone help with this? I'm vaguely familiar with it but not nearly enough to "get it" on my own. To be clear, I am not seeking assistance with html, only the JavaScript function that meets the name parameter:

(must be non-blank, must begin with a capital letter, and must contain at least one vowel and one consonant)

Thank you for your help!

7
  • SO is not meant to get your homework done by someone else. Post the code you've already tried to use and ask a specific question about your problem. Commented Jul 20, 2014 at 20:50
  • w3schools.com. That should get you started. Commented Jul 20, 2014 at 20:51
  • My point is that the JavaScript is not my homework, only something I've been told I could access online but can't seem to find. My homework is implementing it into my html page and making it work. I am allowed to copy/paste any JavaScript I find to make this work as long as I cite the source. Commented Jul 20, 2014 at 20:55
  • This is my code for the html, which is all I really have to add. I just need the JavaScript for the function validateRealname() tr> <td><label for="realname">Name:</label></td> <td><input id="realname" align="left" size="50" name="realname" onchange="validateRealname(this, 'realnameguide');"></input></td> <td id="realnameguide">Please use proper case when entering your name.</td> </tr> Commented Jul 20, 2014 at 20:57
  • @user3533863 - No need for JavaScript if you are just doing simple validation: html5rocks.com/en/tutorials/forms/html5forms/#toc-validation Commented Jul 20, 2014 at 20:59

2 Answers 2

2

In HTML5 there is no need for JavaScript:

<input required pattern="[A-Z](.*[aeiou].*[^aeiou].*|.*[^aeiou].*[aeiou].*)">

http://jsfiddle.net/sfjT9/

If you need to check validity in JavaScript, use this:

yourInput.checkValidity();

W3C specification: http://www.w3.org/TR/html5/forms.html#dom-cva-checkvalidity
WHATWG specification: http://www.whatwg.org/specs/web-apps/current-work/#the-constraint-validation-api:dom-cva-checkvalidity

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, it's also not backward compatible.
@PHPglue - The question tagged HTML5 and it's okay to assume OP does not need backward compatibility.
0

Try this, will return true if validation succeeds:

function validateName(name) { 
    var cap = /^[A-Z]{1}.+$/;
    var con = /[bcdfghjklmnpqrstvwxyz]/i;
    var vow = /[aeiou]/i;
    if(cap.test(name) && con.test(name) && vow.test(name)) {
        return true;
    } else {
        return false;
    }
} 

1 Comment

Thank you very much, this is what I was looking for. I sincerely appreciate it!

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.