-1

hello i tried javascript but it didnt work please help me make this work because i really need it its an activity of us to do at home i need this to redeem myself my classmates belittle me of not a good programmer but i love programming. here is the code:

function validate() {
  var first = document.getElementById('fname').value;
  var second = document.getElementById('lname').value;
  var ck_password = /^[A-Za-z]+$/;
  if (first != ck_password.test(first)) {
    alert("Numeric characters only");
    return false;
  }
  return true;
}
<div class="wrapper">
    <form action="success.php" method="post" >
        <label for="fname">First Name</label>
        <input type="text" name="firstname" id="fname" required />
        <label for="lname">Last Name</label>
        <input type="text" name="lastname" id="lname" required />
        <label for="mob">Mobile</label>
        <input type="text" name="mobile" id="mob" required />
        <label for="ename">EmailAddress</label>
        <input type="text" name="email" id="ename" required />
        <label for="gen">Gender</label>
        <select name="gender" id="gen" required>
            <option value="">--Select Gender--</option>
            <option value="">Male</option>
            <option value="">Female</option>
        </select>
        <label for="add">Address</label>
        <input type="text" name="address" id="add" required />
        <label for="c">City</label>
        <input type="text" name="city" id="c" required/>
        <label for="s">State</label>
        <input type="text" name="state" id="s" required/>
        <input type="submit" name="Submit" value="submit"  onsubmit="return validate()"/>
    </form>
</div>

2 Answers 2

1

Need to say one thing before i'll give you the solution, your question is bad formatted, you have to write the behaviour of your problem, don't believe that you'll find often people who code for you, this is an easy thing so i'll write it.

function validate(){
        var first=document.getElementById('fname').value;
        if(isNaN(first){
            alert("Numeric characters only");
            return false;
        }
    return true;
}

html

<input type="submit" name="Submit" value="submit" method="post" onsubmit="validate();">
    </form>
    </div>
</body>
Sign up to request clarification or add additional context in comments.

3 Comments

i need a condition where it only accepts string also its not working also ur code is not working
isNaN in javascript mean it must be not a number
and reading your code it seems you need to do it to other fields to
0

onsubmit works with <form> elements, you should be doing this

<form action="success.php" method="post" onsubmit="return validate();">

You just need to check once the condition like following

 if(!ck_password.test(first)){ // true if numeric character given
       alert("Enter non-numeric characters only");
      return false;
 }

Demo

7 Comments

hello thank you but i want the message to appear right beside the firstname can u please show me the code im 0% knowledge and im putting all what i will be having... thanks
i dont know y its not working on me O_O and how can i make it red BTW thank you now i got an idea but i need to figure why its not working on me
@ShauiosbhaoiuAousiguosba If you want it red.. look at this
wth is wrong in this i copied ur code but doesnt work on mine
@ShauiosbhaoiuAousiguosba You need to refer to the HTML portion in both the fiddle i've provided, because i've added label after the html input!
|

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.