<input type="text" id ="idOfValueToTest" />
<input type="submit" id ="btnValidate" value="Submit" onclick="return Validate()" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"> </script>
<script type="text/javascript">
function Validate() {
var validation = document.getElementById("idOfValueToTest").value;
var pattern = /^\d{10}$/;
if (pattern.test(validation)) {
return true;
}
return false;
}
</script>
Try this!
I'm sorry i misread java to javascript. The regex should work though. Try:
public static boolean isValid(String input)
{
String regex = "^\d{10}$";
if(input.matches(regex))
{
return true;
}
return false;
}
Hi, i see what you're saying. Well as other have said you should not use stringbuilder to do that. If you're looking for resources then i would suggest reading the class description here: https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html . Basically as the name says, a stringbuilder builds strings! It might be a weird way to actually do what you want using append but that would be really weird and really bad programming. I will add an example of what you should NOT do!
String text = "abcdefghijklmnopq";
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 10; i++)
{
builder.append(Character.toString(text.charAt(i)));
}
String result = builder.toString();
System.out.println(result);
See what other people posted if you're looking a most commonly used way to do this. If regex seems complicates then i would suggest going with .length as that is pretty straight forward, but do some research on regex, is a very good tool!